简体   繁体   中英

MultipleChoiceField django python, impossible display datas in the template

I have big problem, I use MultipleChoic Field into django for "input multiselect", a user at the possibility of make many choices via the "input select multiple"

I explain to you :

I use this Package : https://pypi.org/project/django-multiselectfield/

this is my model :

class Profil(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,) 
skill = MultiSelectField(max_length=500, null=True, blank=True, choices=Skillz)
board = MultiSelectField(max_length=500, null=True, blank=True, choices=Boardz)

this is my form :

class ProfilForm(forms.ModelForm):
skill = forms.MultipleChoiceField(required=False, widget=forms.SelectMultiple, choices=Skillz)
board = forms.MultipleChoiceField(required=False, widget=forms.SelectMultiple, choices=Boardz)

this is my datas of the choices (used in models Profil and form ProfilForm):

Boardz = (
('Reddit', 'reddit.com'),
('Discord', 'discord.com'),
('Twitter', 'twitter.com'),
)

Skillz = (
('Python', 'PythonDjango'),
('Rust', 'RustRocket'),
('Ruby', 'RubyOnRails'),
)

Now the concern problem is views.py and my template.html

 <!-- this is what I want with my datas --> <select multiple class="form-control" id="id_board"> <option>reddit.com</option> <option>discord.com</option> <option>twitter.com</option> </select> <select multiple class="form-control" id="id_skillz"> <option>PythonDjango</option> <option>RustRocket</option> <option>RubyOnRails</option> </select> <input type="submit" value="Save"> 

Into the views.py (which is surely wrong) :

def GetDatasForRegisterForm(request):
form = ProfilForm()
return render_response(request, "registration/register.html",{'form': form})

template.html : I'm totally lost for display my data choices in select multiple for the user, please guys, how make ?

This is how i go about generating my template and views.py when dealing with forms.

First here you have defined your form ProfilForm one quick way to know the html generated in your form when your views look like

def GetDatasForRegisterForm(request):
    form = ProfilForm()
    return render_response(request, "registration/register.html",{'form': form})

In your templates you can just do

<form method="POST" action="<your_action>">
{{ csrf_token }}
{{ form.as_p }}

 <input type="submit" value="Save">
</form>

Now when you render this django will create a basic HTML for you, based on model specified. You can copy the HTML and customize the styling and populate the values by accessing the form field values.

Now you haven't asked about how to save this form. But let me do that anyway.

We are going to handle the form post in the same controller which will look like.

def GetDatasForRegisterForm(request):
    form = ProfilForm(request.POST or None)
    if request.method == "POST" and form.is_valid():
         form.save()
         return redirect("/<sucess_page>")
    return render_response(request, "registration/register.html",{'form': form})

In the view here, i am loading the form with POST data if it exists and checking if the form is valid in the POST and saving it as well. You can where to lead the page to if it is success or error.

Update : It's good, I have what I wanted, here is for your members of stack overflow, my solution :

class ProfilForm(forms.ModelForm):
 skill = forms.MultipleChoiceField(widget=forms.SelectMultiple, choices=Skillz) # skillz and boardz into choices.py 
 board = forms.MultipleChoiceField(widget=forms.SelectMultiple, choices=Boardz)

 class Meta:
    model = Profil
    fields = ('skill', 'board',)

in views.py > form = ProfilForm(), return render..

{% for value,text in form.board.field.choices %}
 <option value="{{ value }}">{{ text }}</option>
 {% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM