I’m trying to display some checkbox in a form after getting the data from my database.
I have some fields that set if something is public or not. The checkbox are always ‘unchecked’.
my class:
class PrivacyForm(forms.Form):
geburtsdatum=forms.CheckboxInput()
strasse=forms.CheckboxInput()
plz=forms.CheckboxInput()
ort=forms.CheckboxInput()
land=forms.CheckboxInput()
fon=forms.CheckboxInput()
my method:
@login_required
def edit_privacy(request,template_name='user/edit_privacy.html'):
user=Users.objects.get(id_u=request.user.id)
if request.method=='POST':
form=PrivacyForm(request.POST)
if form.is_valid():
#
render_to_response('user/public_profil.html',{'profil':user},context_instance=RequestContext(request))
else:
msg="make sure all fields are entered and valid"
else:
#form
form=PrivacyForm()
msg="Select what you want to be public"
return render_to_response(template_name,{'form':form,'profil':user,'msg':msg},context_instance=RequestContext(request))
and my form:
{% extends "home/portal.html" %}
{% block body %}
<h3>::::: PRIVATSPHÄRE :::::</h3>
{{msg}}
<form id="formSearch" action="/user/privacy/" method="post">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Speichern" class="button"/>
</form>
{% endblock %}
Any help would be appreciated. Thanks 🙂
I’m not sure to understand how you use this form, but this will work:
And then in the view code, you can set initial values:
which will render the specified checkboxes as checked(if ‘True’) or unchecked(if ‘False’)..