Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango how to use template tags
    text
    copied!<p>So I want to use this getattribute function (found on this link) <a href="https://snipt.net/Fotinakis/django-template-tag-for-dynamic-attribute-lookups/" rel="nofollow">https://snipt.net/Fotinakis/django-template-tag-for-dynamic-attribute-lookups/</a></p> <p>in my django templates. I created a templatetags folder in my app folder where my models.py is. I also created and saved an empty <strong>inint</strong>.py file in the templatetags folder. I then created a file called getattribute.py in the template tags folder and copy-pasted the snippet found in the link above into the getattribute.py file and saved the file. </p> <p>This is what my template looks like:</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;form method="post" action=""&gt;{% csrf_token %} {{ form.first_name }} {{form.last_name }} &lt;br&gt; {{ form.username }} {{ form.password }} &lt;br&gt; &lt;input type="submit" value="Register"/&gt; &lt;/form&gt; {% load getattribute %} {% for field, error in form.errors.items %} {% if forloop.counter == 1 %} {% with field_obj=form|getattribute:field %} {{ field_obj.label }}{{ error | striptags }} {% endwith %} {% endif %} {% endfor %} &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This is how my models.py looks.</p> <pre><code>class Users(models.Model): alpha_field = RegexValidator(regex=r'^[a-zA-Z]+$', message='Name can only contain letters') user_id = models.AutoField(unique=True, primary_key=True) username = models.SlugField(max_length=50, unique=True) first_name = models.CharField(max_length=50, verbose_name='first Name', validators=[alpha_field]) last_name = models.CharField(max_length=50, validators=[alpha_field]) password = models.SlugField(max_length=50) </code></pre> <p>My forms.py is this.</p> <pre><code>class UsersForm(forms.ModelForm): class Meta: model = Users widgets = {'password':forms.PasswordInput()} def __init__(self, *args, **kwargs): super( UsersForm, self ).__init__(*args, **kwargs) self.fields[ 'username' ].widget.attrs[ 'placeholder' ]="Username" self.fields[ 'first_name' ].widget.attrs[ 'placeholder' ]="First Name" self.fields[ 'last_name' ].widget.attrs[ 'placeholder' ]="Last Name" self.fields[ 'password' ].widget.attrs[ 'placeholder' ]="Password" self.fields['first_name'].label='first Name' </code></pre> <p>and this is my views.py</p> <pre><code>def home_page(request): form = UsersForm() if request.method == "POST": form = UsersForm(request.POST) if form.is_valid(): form.save() c = {} c.update(csrf(request)) c.update({'form':form}) return render_to_response('home_page.html', c) </code></pre> <p>Now, when I run the server, the form is displayed with no errors. However, if I purposely not fill out the first name section and hit submit, it says "This field is required." I want it to say the verbose name and the snippet is supposed to make it say the verbose name if, in my template, I use {{ field_obj.label }}, right? But it is not display the verbose name for some reason. I'm guessing it is because I'm not using the templatetags properly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload