How to get form fields' id in Django - django

Is there any way to get the id of a field in a template?
In the HTML I get: <input name="field_name" id="id_field_name"...
I know I can get the name with {{ field.html_name }}, but is there anything similar for getting the id?
Or can I only get it like this: id_{{ field.html_name }}?

You can get the ID like this:
{{ field.auto_id }}

You can also use id_for_label:
{{ field.id_for_label }}

This doesn't work for every form field.
For instance {{ form.address.auto_id }} works while {{ form.address.auto_name }} will not.
However you can use {{ form.address.html_name }} to get the equivalent answer.
Here are the docs

From the documentation-
each form field has an ID attribute set to id_<field-name>
, which is referenced by the accompanying label tag. This is important in ensuring that forms are accessible to assistive technology such as screen reader software. You can also customize the way in which labels and ids are generated.
So I would like to say id_field-name , you collect the field name from the model.
Here is the link to the documentation

In Django 2 you can retrieve the ID for a specific field using {{ field.id_for_label }}
This is documented here.

Related

Flask url_for in form action

I have a wtform in my html file, something like this:
{{ wtf.quick_form(form, action="/add/", method="post", extra_classes="form-horizontal",
role="form", form_type="basic") }}
(I actually notice I can omit action). So this works.
Now - if I change it to
action="{{ url_for('add') }}"
, I'm ending up in this url:
http://127.0.0.1:5000/add/%7B%7B%20url_for('add')%20%7D%7D
If I just create a link in html like so:
link
It does work and it gets me to /add/. I was wondering what the difference is.
Thanks!
instead of
action="{{ url_for('add') }}"
try to assign the output of url_for('add') function/helper to action
action=url_for('add')
without {{ and }}
so
{{ wtf.quick_form(form, action=url_for('add'), method="post", extra_classes="form-horizontal", role="form", form_type="basic") }}
Remember not to put commas in the tags ;)

Is there a quick way to grab a the ending of current page url? Django

I have a page. www.example.com/chat/testuser
I want to grab the username (testuser). I'm doing this within the template in order to pass along in an ajax call.
Is there a simple way? Something like {{ request.path }} (this returns full path).
{{ request.user }} might do the trick.
Edit:
Here's are some solutions using request.path. Assuming it returns the string "/chat/testuser", you can use one of these filters:
{{ request.path|cut:"/chat/" }}
{{ request.path|slice:"6:" }}
to remove the unwanted portion.

Django bootstrapform render field and lable separate in template

I am using Django 2.0.4 with bootstrapform https://github.com/tzangms/django-bootstrap-form . I would like to render my field and label separately in my template.
Currently I can render the field using:
{% load bootstrap %}
{{ form.fieldname|bootstrap }}
I would to find a way to separate the render of the label from the field so that I can insert text next to the label for selecting tool-tip. Is anyone able to point me in the right direction to achieve this? Any help will be much appreciated!
Try the following : This link provides more info
{{ form.field_name }} will render the field
{{ form.field_name.label }} will render the label
{{ form.field_name.id_for_label }} the ID

Querying a single field within a Jinja2 template

I'm working on an application where users can enter work requests, and subsequently go in and search for requests. The issue I'm having is building out the request summary screen. Basically, it lists the results of a search query. The snippet from the template is as such:
{% for req in workrequests %}
{{ req.id }} {{ req.customer }} {{ req.dateEntered }} {{ req.Contact }}
{% endfor %}
Here's wher I get hung up. The req.customer, and req.Contact fields are just keys for the customer and contact databases. I want to display the customer name and contact name. I assume the following queries should do the trick:
Customer.query.filter_by(req.customer).one()
Contact.query.filter_by(req.Contact).one()
Problem is, that will return the entire record. I'm just after the name field from both of those tables to put into my template, which is where the problem is. I'm not sure how to proceed with this
Customer.query is a short form of db.session.query(Customer). Use the latter to query for specific columns:
db.session.query(Customer.name).filter_by(id=req.customer).one()
Although I think that what you really want is a single query that gets all the data at once. Something like:
db.session.query(
WorkRequest.id,
WorkRequest.dateEntered,
Customer.name,
Contact.name
).join(
Customer,
Contact
).filter(...)

Django - use template tag and 'with'?

I have a custom template tag:
def uploads_for_user(user):
uploads = Uploads.objects.filter(uploaded_by=user, problem_upload=False)
num_uploads = uploads.count()
return num_uploads
and I'd like to do something like this, so I can pluralize properly:
{% with uploads_for_user leader as upload_count %}
{{ upload_count }} upload{{ upload_count|pluralize }}
{% endwith %}
However, uploads_for_user leader doesn't work in this context, because the 'with' tag expects a single value - Django returns:
TemplateSyntaxError at /upload/
u'with' expected format is 'value as name'
Any idea how I can get round this?
You could turn it into a filter:
{% with user|uploads_for as upload_count %}
While a filter would still work, the current answer to this question would be to use assignment tags, introduced in Django 1.4.
So the solution would be very similar to your original attempt:
{% uploads_for_user leader as upload_count %}
{{ upload_count }} upload{{ upload_count|pluralize }}
Update: As per the docs assignment tags are deprecated since Django 1.9 (simple_tag can now store results in a template variable and should be used instead)
In Django 1.9 django.template.Library.assignment_tag() is depricated:
simple_tag can now store results in a template variable and should be used instead.
So, now simple tag we can use like a:
It’s possible to store the tag results in a template variable rather
than directly outputting it. This is done by using the as argument
followed by the variable name. Doing so enables you to output the
content yourself where you see fit:
{% get_current_time "%Y-%m-%d %I:%M %p" as the_time %}
<p>The time is {{ the_time }}.</p>