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.
Related
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.
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/
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>
I am trying to pass some data between pages, but it's not working. Any tips? I click submit and it takes me to a blank page. If I refresh then it shows my base template styles, but no data is passed.
index.html
{% extends "polls/base.html" %}
{% block title %}Vote{% endblock %}
{% block content %}
<h1>Welcome</h1>
<form action="/polls/" method="post">{% csrf_token %}
<p><label for="pin">Enter group pin:</label>
<input id="pin" type="text" name="pin" maxlength="4" />
<input type="submit" value="View Polls" /></p>
</form>
Moderator login
</p>
{% endblock %}
polls/index.html
{% extends "polls/base.html" %}
{% block title %}Recent Polls{% endblock %}
{% block content %}
{{ pin }}
{% endblock %}
polls/urls.py
url(r'^$',
ListView.as_view(
model=Poll,
template_name='polls/index.html')),
You need to write a view to handle the form submision. The view that works, is listing all your Polls. I recommend you to read the tutorial:
Basic views: https://docs.djangoproject.com/en/1.4/intro/tutorial03/
Form submission: https://docs.djangoproject.com/en/1.4/intro/tutorial04/
Basically, your form will send the data to another URL that will handle the processing of your data.
You must specify it in the action attribute:
<form action="/polls/create-poll" method="post">{% csrf_token %}
<input type='text' name='poll-name' />
<input type='submit' />
</form>
and in your views:
def create_poll(request):
poll_name = request.POST.get('poll-name')
poll = Poll.objects.create(name=poll_name)
return HttpResponse("Poll created")
I don't want to sound rude. But you should start with some HTTP and HTML tutorial. A good web programmer is the one that knows the basic stuff in detail. HTTP is a great protocol, try to learn it all the way through.
In my base.html I placed this:
{% if user.is_authenticated %}
you are logged in!
{% else %}
<h3>Login</h3>
<form action="/login/" method="post" accept-charset="utf-8">
<label for="username">Username</label><input type="text" name="username" value="" id="username" />
<label for="password">Password</label><input type="password" name="password" value="" id="password" />
<p><input type="submit" value="Login →"></p>
</form>
{% endif %}
In urls.py:
(r'^login/$', 'django.contrib.auth.views.login'),
(r'^logout/$', 'django.contrib.auth.views.logout'),
When I accessed /login I had to make a login.html file. I created templates/registration/login.html:
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p>Your username/pass didnt match</p>
{% endif %}
{% endblock %}
I'm not seeing the username/pass, but I'm still seeing the meaning my user is not authenticated yet.
Btw, I don't have the CSRF middleware loaded. Did I miss a step or two?
One other thing, I accessed logout.html and it went into my django admin logout page. I tried making a templates/registration/logout.html but it didn't override that part. Hrm?
Are you passing the user variable to your template context? Either explicitly, or via a context processor?
Doh. Apparently my actual template had action="/login" but when I typed it in the example here it had an end slash, I re-added the end slash and that was it.