Django Formset - Access the primary key id input - django

I want to render primary key id hidden input (<input type="hidden" name="form-0-id" value="5" id="id_form-0-id">) directly in my template. I know I can render whole form, but I don't want to have any labels there. Is there some way to get it using the formset? I used object initial.id, but it didn't work.
Method:
def getCellEditForm(self):
CellFormSet = modelformset_factory(Cell, extra=0, max_num=0)
form = CellFormSet(queryset=Cell.objects.filter(pk=self.id))
return form
Template:
{{ child.getCellEditForm.form.title }}
{{ child.getCellEditForm.form.parent }}
{{ child.getCellEditForm.form.initial.id }}

{% for subform in formset.forms %}
{{ subform.id }}
{% endfor %}
or
{% for subform in formset.forms %}
<input type='hidden' id='id_form{{subform.id}}' name='form{{subform.id}}' value='{{subform.id}}' />
{% endfor %}

Related

csrf_token missing and how to get details for specific id entered by user

I am trying to display the result for the specific id entered by the user. I 'm not sure about the view.py file also. What changes should I need to make to get the desired result?
view.py file
def show(request):
if request.method == 'POST':
Num = allData.objects.only('emp_no')
data = request.POST.get('emp_no')
if data.is_valid():
for n in Num:
if n == data:
empid = data
emp = {'emp_no':data}
return render(request,'system/show.html',{'emp_no':data})
return(data.errors)
return HttpResponse("<h2>OOPS!! NO RECORD FOUND</h2>")
show.html
{% extends 'system/base.html' %}
{% load staticfiles %}
{% block body_block %}
<div class="container" "jumbotron">
<h2>Details</h2>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
{% for allData in emp_no %}
{{ allData.GENDER_CHOICE}}
{{ allData.first_name }}
{{ allData.last_name }}
{{ allData.birth_day }}
{{ allData.hire_date }}
{{ allData.dept_no }}
{{ allData.dept_name }}
{{ allData.salary }}
{{ allData.from_date }}
{{ allData.to_date }}
{{ allData.titles }}
{% endfor %}
</div>
{% endblock %}
Welcome to SO!
From what you have there it looks like it would be worthwhile taking a look at the Django forms documentation.
The main thing that jumps out to me is that you are trying to reuse the same template both for the form and for the data display after the form is submitted. It might be easier to separate them.
If you do want to keep one template, then you don't want to show the form if there is data and vice versa - if there is no data, then you want to show the form. It would look something like this:
{% if emp_no %}
{% for allData in emp_no %}
{{ allData.GENDER_CHOICE}}
{{ allData.first_name }}
{{ allData.last_name }}
{{ allData.birth_day }}
{{ allData.hire_date }}
{{ allData.dept_no }}
{{ allData.dept_name }}
{{ allData.salary }}
{{ allData.from_date }}
{{ allData.to_date }}
{{ allData.titles }}
{% endfor %}
{% else %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
{% endif %}
The title of your question also mentions csrf_token problems. You didn't give any details, but my guess is things are getting confused because you are loading the form even when you don't need it.
I will point out a few things before trying to post a solution.
Your code is not that readable because of the way you name variables. Try naming your variables in a way that tries to reflect what your code is doing.
I will suggest that you finish django tutorial if you haven't done that already. It will help you grab the core concepts of Django.
From what I understood you are trying to get an employee with a given employee number.
This is how I would do it.
In the views.py I would user render instead of HttpResponse.
def show(request):
context = {}
if request.method == 'POST':
# extract the emp no from the request
emp_no = request.POST.get('emp_no')
# get or create the employee with this emp_no
current_employee = Employee.objects.get_or_create(emp_no)
context['current_employee'] = current_employee
return render(request, 'show.html', context)
In the templates, you don't need a forloop. You are retrieving only one employee.
{% extends 'system/base.html' %}
{% load staticfiles %}
{% block body_block %}
<div class="container" "jumbotron">
<h2>Details</h2>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label class="lb" for="emp_no" >Employee No.</label>
<input type="number" name="emp_no">
<button type="submit" class="btn btn-success">SUBMIT</button>
</form>
<div>
{{ current_employee.GENDER_CHOICE}}
{{ current_employee.first_name }}
{{ current_employee.last_name }}
{{ current_employee.birth_day }}
{{ current_employee.hire_date }}
{{ current_employee.dept_no }}
{{ current_employee.dept_name }}
{{ current_employee.salary }}
{{ current_employee.from_date }}
{{ current_employee.to_date }}
{{ current_employee.titles }}
</div>
</div>
{% endblock %}

Django: For Loop to Iterate Form Fields

I don't want to use django's built in form generation, seeking to specify each field in my template in order to customize the html output.
How do I iterate over a series of form fields?
If my form looks like this:
class MyForm(forms.Form):
main_image = forms.ImageField()
second_image = forms.ImageField()
third_image = forms.ImageField()
fourth_image = forms.ImageField()
...
Is there way to write a {% for %} loop so that I can iterate through:
{{ form.main_image }}
{{ form.second_image }}
{{ form.third_image }}
{{ form.fourth_image }}
I tried the following which seemed logical, but did not work:
{% for field in form %}
{{ form.field }}
{% endfor %}
Well this would clearly not work:
{% for field in form %}
{{ form.field }}
{% endfor %}
but this will:
{% for field in form %}
{{ field }}
{% endfor %}
The best way is to use two loops, one for hidden fields and one for visible fields :
visibles:
{% for field in form.visible_fields %}
{{ field.label }}
{{ field }}
{% endfor %}
hiddens:
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
in this way you will have better control over UI elements.
This one should work :
{% for field in form %}
{{ field }}
{% endfor %}
Once you loop through field in form , you can't access form.field
For any frontend developers looking to customize a Django form you can use a package called django-widget-tweaks to render fields individually. Example code below:
{# Your template with the form #}
{% extends "base.html" %}
{% load widget_tweaks %}
<form action="" method="POST">
{% csrf_token %}
{% for field in form %}
<label for="{{ field.id_for_label }}">
{{ field.label }}{% if field.field.required %}*{% endif %}
</label>
{% render_field field %}
{% endfor %}
<button type="button">
Submit Form
</button>
</form>
Note: You'll want to make this look nicer of course, and you may want to loop through your form errors if there are any.
They have some very useful examples on their PyPi page as well.

Only using django forms for validation

After using django forms they appear great for validation but I'm still not liking the way the fields are displayed.
Is it poss to only use the form fields for validation.
I.e how do you pass just the form fields to the template ?
Thanks
you can iterate the form and get each field seperately and do whatever you want to do:
<form action="/contact/" method="post">
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
Hope this somehow helps ...

Trying to access ModelForm field modelChoice choices in Django template

I'm generating ModelForms and want some granular control over how they are output in my template. Specifically, I need to add some markup to the end of each radio button in each of my select lists.
Code:
# order-form.html
{% load catname %}
<form id = "order-form">
{% for form in forms %}
<div id="gun-{{ forloop.counter }}">
{% for field in form.fields %}
<div id="{{ field }}-item" class="item">
<h3>{{ field|catname }}</h3>
{% for choice in form.field.choices %} {# <-- Help me out here #}
{{ choice.id }}
{{ choice.title }}
{% endfor %}
</div>
{% endfor %}
{% endfor %}
<button type="submit" id="standard-gun-form-submit">Continue to next step</button>
</form>
# views.py
def get_form(request):
if request.method == 'POST':
if request.POST['gun_type'] == 'standard':
forms = [StandardGunForm(prefix=p) for p in range(0,2)]
return render_to_response('main/order-form.html', {'forms' : forms,}, RequestContext(request))
# forms.py
class StandardGunForm(ModelForm):
def __init__(self, *args, **kwargs):
super(StandardGunForm, self).__init__(*args, **kwargs)
for field in self.fields:
if isinstance(self.fields[field], ModelChoiceField):
self.fields[field].empty_label = None
class Meta:
model = BaseGun
widgets = {
'FrameTuning' : RadioSelect(),
'FrameConnection' : RadioSelect(),
}
exclude = ('price')
Endgame: markup that looks like this
<form id="foo">
<div class="category">
<div class="item">
<input type="radio" name="srsbzns" value="1">Option 1</input>
<img src="http://placekitten.com/150/150">
<p>Other foo here</p>
</div>
<div class="item">
<input type="radio" name="srsbzns" value="2">Option 2</input>
<img src="http://placekitten.com/150/150">
<p>Other foo here</p>
</div>
<div class="item">
<input type="radio" name="srsbzns" value="3">Option 3</input>
<img src="http://placekitten.com/150/150">
<p>Other foo here</p>
</div>
</div>
</form>
From the shell, this returns what I want
>>> forms = [StandardGunForm(prefix=p) for p in range(0,2)]\
>>> forms[0].fields['frame_tuning'].choices.queryset
I'm surprised this is proving so challenging!
Bonus: I have DEBUG = True and Django Debug toolbar enabled. Is it possible to dump the variables to the browser, so I can see what this stuff looks like as I drill down?
Thanks!
I had to do something similar and started down this path as well. I wanted to create table rows from a ModelChoiceField where each column had a different field of the model instance (and then I'd allow filtering the table rows via JavaScript).
I couldn't find it in the Django docs, but a quick perusal of the Django source showed the way. You can get to the queryset to access the model instances like so:
<form action="{% url 'some_view' %}" method="post">
{% csrf_token %}
{% if form.non_field_errors %}
{{ form.non_field_errors }}
{% endif %}
{% for field in form %}
{{ field.label }}
{% if field.field.choices %}
{% for model_instance in field.field.choices.queryset %}
{{ model_instance.id }}
{% endfor %}
{% else %}
{{ field }}
{% endif %}
{% if field.errors %}
{{ field.errors|striptags }}
{% endif %}
{% endfor %}
<button type="submit">Submit</button>
</form>
However, at this point we've disassembled the shiny widget (in my case a CheckboxSelectMultiple) and must re-assemble the HTML form input using template code. I found no direct way to simultaneously iterate over the ModelChoiceField to access the model instance fields and get the HTML form tags for the choices.
Maybe there's a way, but I abandoned my attempt and built my own HTML form, handling all the POST data in a view. It ended up much easier that way. ModelForms are really nice and convenient, but using them for something they weren't built for can end up being more difficult.
I figured I'd post this in case anyone is trying to do it for some other reason. Hope it helps.
Very late, but I'm reading now and this is what it worked for me
{% for field in form %}
{% for x, y in field.field.choices %}
{{x}}
{{y}}
{% endfor %}
{% endfor %}
Where "x" is the id or code, and "y" is the readable value or title.
You can access the underlying model instance for each choice:
{% for choice, label in form.field_name.field.choices %}
{{ choice.value }}
{{ choice.instance }}
{{ choice.instance.instance_attribute }}
{{ label }}
{% endfor %}
{% for choice in form.field.choices %} {# <-- Help me out here #}
{{ choice.id }}
{{ choice.title }}
{% endfor %}
Look what you're doing here, you're literally trying to access a field called "field" every time in this loop, which presumably does not exist.
You need to take the field object you're iterating through, and access the choices attribute on that.
{% for field in form.fields %}
{% for choice in field.choices %}

django change form field in .html template output

I create a form based on a model , like seen below:
class ContactSelectionForm(forms.ModelForm):
contacts = ManyToManyByLetter(Contact, field_name="first_name")
class Meta:
model = ContactSelection
exclude = ('created_by',)
When I process this view I see at the .html output a field labeled with "Contact".
Now I`m wondering whether it is possible to change this output. For example I want to name this field not "Contact" but "Selected Contacts".
This is the form processing part of the .html template:
<form action="{{ request.path }}" method="POST">
<div>
<fieldset class="module aligned">
{% for field in form.visible_fields %}
<div class="form-row">
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Save" /></p>
</fieldset>
</div>
</form>
If somebody is wondering what ManyToManyByLetter(Contact, field_name="first_name") in the form is, check out http://code.google.com/p/django-ajax-filtered-fields/ . A very helpful many2many javascript library.
Did you try setting the fields label?
(the docs)
contacts = ManyToManyByLetter(Contact, field_name="first_name", label="Selected Contacts")