0

I created a CheckboxSelectMultiple form for my Django app. When a user selects the choices, I want to show it on the 'details' page. But when I make it, it displays <django.forms.fields.CharField object at 0x7faf189cad10> How can I fix it and display the output of the choice of the user?

models.py

from __future__ import unicode_literals
from django.utils import timezone
from django.db import models
from django import forms

LANG_CHOICES = (
    ('java', 'JAVA'),
    ('c/c++', 'C/C++'),
    ('python', 'PYTHON'),
    ('html', 'HTML'),
    ('css', 'CSS'),
    ('javascript', 'JAVASCRIPT'),
)


class Project(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=100)
    project_name = models.CharField(max_length=300)
    project_description = models.TextField()
    project_notes = models.TextField()
    select_langs = forms.CharField(label='dil sec: ', widget=forms.Select(choices=LANG_CHOICES))
    created_date = models.DateTimeField(
        default=timezone.now)
    published_date = models.DateTimeField(
        blank=True, null=True)

views.py

def project_details(request, pk):
project = get_object_or_404(Project, pk=pk)
return render(request, 'blog/project_detail.html', {'project': project})
...                
def project_new(request):
if request.method == 'POST':
    form = ProjectForm(request.POST)
    if form.is_valid():
        project = Project()
        project.first_name = form.cleaned_data['first_name']
        project.last_name = form.cleaned_data['last_name']
        project.email = form.cleaned_data['email']
        project.project_name = form.cleaned_data['project_name']
        project.project_description = form.cleaned_data['project_description']
        project.project_notes = form.cleaned_data['project_notes']
        project.published_date = timezone.now()
        project.lang_choices = form.cleaned_data['select_lang']
        project.save()
        return redirect('project_detail', pk=project.pk)
else:
    form = ProjectForm()
return render(request, 'blog/project_new.html', {'form': form})

forms.py

    ...   
 select_lang = forms.CharField(label='languages: ', widget=forms.CheckboxSelectMultiple(choices=LANG_CHOICES))

project_details.html

    <h1> The programming languages that user's will use:  {{project.select_langs}} </h1>
Jode
  • 111
  • 2
  • 12

1 Answers1

0

You are mixing model definition with form definition in your model

...
select_langs = forms.CharField(
    label='dil sec:', # Wrong: this a form attribute
    widget=forms.Select(choices=LANG_CHOICES)) # Wrong: this a form attribute
...

label and widget are form attributes, not models'.

Models are representations of tables in a database, so there you don't define how a form is constructed.

BUT:

You are facing a problem regarding how to store the data, since you want to save multiple options into the database:

Check Django Model MultipleChoice

schrodingerscatcuriosity
  • 1,780
  • 1
  • 16
  • 31