I want to render a form in Django. In normal case, you access fields like this:
{{ form.name }}
But I'm using date field with SelectDateWidget widget. This widget has different field for year,month and day.
How could I access these fields one by one?
{{ form.date.year }} or
{{ form.date.0 }} or
{{ form.date_year }} etc. doesn't work.
Try this:
{{ form.date|date:"Y" }}
You can take a look at the complete docs here: https://docs.djangoproject.com/en/1.10/ref/templates/builtins/
Related
I have an inline formset which is working fine. There are a number of select boxes in the formset that lets you pick from various options in related tables.
QuantityFormSet = inlineformset_factory(Option, Quantity, fields=('item', 'number','area'),extra=1)
Item and Area are related tables.
I want to set up a button inline with the form that let's the user click through to edit these two objects.
So, for example, I want to achieve something like.
{% for form in quantityForm %}
{{ form.item }}
<i class="fas fa-edit"></i>
{{ form.number }}
{{ form.area }}
{{ form.DELETE }}
{% endfor %}
However form.item.id isn't valid.
How do I get the ID for form.item (which is a select box)?
Have you tried {{ form.instance.item.id }}?
I am implementing the very cool third party package Django-Simple-History into a project. Per the documentation I'm using the middleware to store which user makes changes to an object. This makes it easy to iterate over in the template like:
{% for x in object.history.all %}
{{ x.history_date }}, {{ x.history_user_id }} <br />
{% endfor %}
I am trying to use the available user.id to get the correlating user.username in the template. Any suggestions? (I'm still pretty new to Django/Python) Thanks!
history_user holds the ForeignKey for the related user.
You can use: {{ x.history_user.username }}
I am trying to sort objects passed to django, and I am passing them in like this:
{% for attendee in attendees|dictsort:"last_name" %}
{{ attendee.first_name }} {{ attendee.last_name }}
dictsort isn't working though, because they are objects and not a dictionary. How do I sort these? The goal is to have an angularjs button that you can click and it will change the sorting parameter like:
dictsort:{[{clickedTab}]}
How do i do specify the many database field when in a wtf form, so i can insert a row in the database correctly. I need something like this in my template
{{ wtf.form_field(gform.GHF(value="{{ project.name }}")) }}
because I'm iterating over one (Projects) to many (Goals)
Project-(has many goals)
-goal-
and my goal form shows up multiple times.
{% for project in P %}
{% for pgoal in project.goals.all() %}
<li>
Goal: {{ pgoal.goal }}<br>
{% if loop.last %}
<form class="form form-horizontal" method="post" role="gform">
{{ gform.hidden_tag() }}
{{ wtf.form_errors(gform) }}
{{ wtf.form_field(gform.goal) }}
Help here? do i need a hiddenfield to know which project?
{{ wtf.form_field(gform.submit) }}<br>
and so on...
Once I have the correct project, I will use it in my view here
u=models.Projects.query.get(correct project?)
p=models.Goals(goal=gform.goal.data,proj=u)
I wouldn't do it with a hidden field. I'd make each form submit a little differently.
You should have something like
<form class="form form-horizontal" method="post" role="gform"
action="{{ url_for('add_goal_to_project', project_id=project.id) }}">
And the route would be
#app.route('.../<int:project_id>', methods=['POST'])
def add_goal_to_project(project_id):
gform = GForm(....)
if gform.validate_on_submit():
project = models.Projects.query.get(project_id)
goal = models.Goals(gform.goal.data, proj=project)
# Do anything else you need to do, such as adding and committing
# the new object
return redirect(...)
return render_template(...)
I'm skipping the details in the form creation, redirect and render_template calls, but this should get the idea across. Each goal form's action points to a route built from the project id.
You could extend this to allow for the editing of goals, and you'd be able to make it a lot better with some nice ajax posts as well.
I have one form :
class FormLogin(forms.Form):
email = forms.EmailField(max_length=150)
name = forms.CharField(max_length=20)
How can I put just email field in my template ?
I tried this :
{{ form.fields.email }}
But it returns <django.forms.fields.CharField object at 0x00000000043BCEB8>.
You can just use:
{{ form.email }}
You don't need to use fields.
Use:
{{ form.email }}