Placement of {% if form.errors %} on login template - django

I'm not sure if I'm allowed to ask these types of questions here since it's not a problem per say, so please let me know. But I was wondering for the login.html template on Django:
{% extends 'learning_logs/base.html' %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method = "post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name = "submit">log in</button>
<input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
</form>
{% endblock content %}
How come it checks for the form.errors before the it even processes the form? Thanks for any help on this question.

The template ordering doesn't define how the form logic works, only how certain items are displayed. You could put the {% if form.errors %} block after the <form> and there would be no difference to how the form is processed by Django. Furthermore, form.errors will only exist once a form has been validated with is_valid() and any errors exist.

Related

How to solve 'This field is required' for django forms

I created a blog like app. Whenever I update an entry and press the submit button, I want it to take me to view page for that blog entry. However it just redirects me to the update page. When I check form.errors it says that 'This field is required' for all of the fields. But all of the fields are filled with data.
{% extends 'base.html' %}
{% block content %}
{{ form.as_p }}
<form method="POST" action="">
{% csrf_token %}
<input type="submit" value="Update">
</form>
{% endblock content %}
You have your form declared in the wrong block:
{% extends 'base.html' %}
{% block content %}
<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update">
</form>
{% endblock content %}
Notice that the {{form.as_p}} has to be declared into the form block.
If you fill all data but your form is out of the form tag then the data will not be send.

Django Registration Registration_form

I'm using Django Registration_redux templates for user registrations.
I am wondering how does Django know where to go when user submits his registration when action is action="." ?
{% extends "registration/registration_base.html" %}
{% load i18n %}
{% block title %}{% trans "Register for an account" %}{% endblock %}
{% block content %}
<form method="post" action="">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
{% comment %}
**registration/registration_form.html**
Used to show the form users will fill out to register. By default, has
the following context:
``form``
The registration form. This will be an instance of some subclass
of ``django.forms.Form``; consult `Django's forms documentation
<http://docs.djangoproject.com/en/dev/topics/forms/>`_ for
information on how to display this in a template.
{% endcomment %}
If your action is blank or . then the post goes to the view (URL) which rendered the form.
Here's a simple example using a contact form; https://hellowebapp.com/news/tutorial-setting-up-a-contact-form-with-django/

NoReverseMatch on django generated views

I'm trying to create the views for all the authentication process in Django, but I have an issue with the reverse url tag in the views.
I have :
urls.py
url('^v1/back/', include('django.contrib.auth.urls'))
login.html
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="{% trans 'Log in' %}" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<p>{% trans "Forgot password" %}? {% trans "Reset it" %}!</p>
<p>{% trans "Not member" %}? {% trans "Register" %}!</p>
{% endblock %}
And when I access localhost:8000/v1/back/login/ i have:
NoReverseMatch at /v1/back/login/
Reverse for 'django.contrib.auth.views.auth_password_reset' with
arguments '()' and keyword arguments '{}' not found. 0 pattern(s)
tried: []
The thing is, as, you can see, i try putting the complete path, it still does not work.
When I try to access another view like password change, it redirects me to :
http://192.168.56.103:8000/accounts/login/?next=/v1/back/password_change/
Which obviously does not work, and when i access password reset :
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
It has the django admin look and feel instead on my base.html.
I'm guessing there are lots of problem here, I'm trying to solve them one by one, but i don't know what i've done wrong on the url part.
Don't know if this is relevant, but a part of my application is served by Django Rest Framework, and i'm trying to put in place oAuth2.
The correct name for password reset is just password_reset, to reverse it use:
{% url 'password_reset' %}
To fix the login redirect, you have to adjust the LOGIN_URL in your settings.py
I suggest you remove the part regarding the template and post it as a separate question.

Error loading sample django-bootstrap3 template

I'm still new to Django and Bootstrap so I'm trying out the django-bootstrap package: https://github.com/dyve/django-bootstrap3
The sample template that is included on that page (with a change of url in form action):
{% load bootstrap3 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}
{# Display a form #}
<form action="/search/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% bootstrap_form_buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% end_bootstrap_form_buttons %}
</form>
Gives me the error:
BootstrapError at /
Parameter "form" should contain a valid Django Form.
on this line
{% bootstrap_form form %}
I'm not exactly sure what's the problem is since this is the sample template that's included in that README.
{% bootstrap_form form %} is a template tag provided by django-bootstrap3 that expect a django form instance, so the "form" parameter is the context variable mentioned in displaying-a-form-using-a-template from django docs.
Create the form as explained in that page, and then replace the html code they use in template:
<form action="/contact/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
by the sample code in your question.
Now Parameter "form" contains a valid Django Form
Hope this helps
You just need to provide an object form server side, which must have a context name "form".
In your views.py, include something like this
from django.shortcuts import render
def index(request):
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
template = "your_template.html"
context = { "form" : NameForm() }
return render( request, template, context )
Now you should not have any error.
Hope it helps
try this
{# Load the tag library #}
{% load bootstrap3 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}
{# Display django.contrib.messages as Bootstrap alerts #}
{% bootstrap_messages %}
{# Display a form #}
<form action="/url/" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" class="btn btn-primary" value="Submit" />
</form>
You don't really need the bootstrap_button tags. I tried to find them as well, but they are not declared in the source...
Put {% extends 'bootstrap3/bootstrap3.html' %} at the beginning of your snippet. It's supposed to be your file, bootstrap3.html is at this placeholder.
Error is pretty straightforward, make sure you pass valid django form. I was passing form.as_p() instead of form in my view and got this error. Took me a while to notice. May be it will still help somebody.
for django 1.8 use {{ form }} instead of {{ form.as_p }} as in django 1.6, for this minor change can cause an error
please see django 1.8 official documentation:
https://docs.djangoproject.com/en/1.8/topics/forms/#the-template
{% load bootstrap3 %}
{# Display a form #}
<form action="/submit/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>

How do I customize Django's comments form?

I am trying to customize Django's comments form. Inside django.contrib.comments.forms I noticed that all the field forms are declared in the class CommentDetailForm, which is inherited from CommentSecurityForm. Then I think when I write the template tag {% get_comment_form for order as form %}, it's getting the class called CommentForm which inherits CommentDetailForm with a honeypot field.
I wanted to customize the comments form so that it only displays the comments field (and not the optional name, email, or URL fields). Those information will be given by the current logged in user. In fact, only logged in users with certain UserProfile.user_type (UserProfile has a foreign key to User) are allow to comment.
Any tips on how to achieve this? Looking at the source code of the Django's comments already scares me lol.
EDIT:
Here is how the comment template looks so far:
{% get_comment_form for order as form %}
<form action = "{% comment_form_target %}" method = "post">
{% csrf_token %}
{{ form }}
<input type = "submit" name = "submit" value = "Post">
</form>
And the site looks like this
I want to hide Name, Email address, and URL.
You should be able to do all of this in the template:
{% ifequal User.profile.user_type "comment_type" %}
{% get_comment_form for order as form %}
<form action="{% comment_form_target %}" method="post">
{% csrf_token %}
{% for field in form %}
{% ifequal field.name "name" %}
<input id="id_name" type="hidden" name="name" value="{{ user.username }}" />
{% else %}{% ifequal field.name "email" %}
<input type="hidden" name="email" value="{{ user.email }}" id="id_email" />
{% else %}{{ field }}{% endifequal %}{% endifequal %}
{% endfor %}
<input type="submit" name="submit" value="Post">
</form>
{% endifequal %}