Flask url_for in form action - flask

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 ;)

Related

Is there a way to use django url template in json_script?

trying to find a way to save a url into a json script but its not rendering and reading it literally unlike other values:
Tried this way
{{ "{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date%}" | json_script:"url_test"}}
And this way:
{{ url_test | json_script:"{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date%}"}}
But still comes out like this:
<script id="url_test" type="application/json">"{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date%}"</script>
You can store this in a variable:
{% url 'tutorhomepage:tutorSelectedDay' tutor_id selected_date as myurl %}
{{ myurl|json_script:"url_test" }}

Django puts spaces in first Line of TextField

I'm creating a webapp where I can present a documentation. In my documentations I often have some code snippets so I used prism (https://prismjs.com/) to format the text I enter in a textfield on the admin page.
The Problem is that whenever I enter code in the textfield it makes spaces on the first line like that:
import { Pipe, PipeTransform } from '#angular/core';
import { UserService } from '../user.service';
My code looks likt this:
Template:
<pre><code class="language-{{ subdoc.language }}">
{{ subdoc.code }}
</code></pre>
Remove the spaces/tab characters before {{ subdoc.code }}
You must be exhausted in coding. Most of the time we think the complicated coding is where something's wrong but in your case it's the HTML.
you sure know how <pre> tag works. Now understand the difference between this
<pre><code class="language-{{ subdoc.language }}">
{{ subdoc.code }}
</code></pre>
and this
<pre><code class="language-{{ subdoc.language }}">
{{ subdoc.code }}
</code></pre>
Happy coding ;)

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.

Remove minus sign from number in django tmplate

In my template I have this:
{{ gainer.total_progression }}
Which produces -5664
I need 5664
I tried
{{ gainer.total_progression }}|slice:"1:"
but it won't remove the -.
What is the proper way to handle this?
First, your code is wrong, it should be:
{{ gainer.total_progression|slice:"1:" }}
Also, you could use cut instead of slice:
{{ gainer.total_progression|cut:"-" }}
If you're not against installing separate library for this, you can use django-mathfilters.
{{ gainer.total_progression|abs }}

How to get form fields' id in 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.