I am on the Django tutorial (part 4) and trying to create a form which allows user to choose answer to a poll. The questions load properly, however when I click 'vote' (ie select option and submit form) the following error keeps showing:
Page not found (404)
Request Method: POST
Request URL: http://localhost:8000/polls/vote/6
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/ ^$ [name='index']
^polls/ ^(?P<poll_id>\d+)/$ [name='detail']
^polls/ ^(?P<poll_id>\d+)/results/$ [name='results']
^polls/ ^(?P<poll_id>\d+)/vote/$ [name='vote']
^admin/
The current URL, polls/vote/6, didn't match any of these.
Here is the code from detail.html which has the form:
{{ poll.question }}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/polls/vote/{{ poll.id }} " method="post">
{% csrf_token %}
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>
I suspect the problem is in the line <form action="/polls/vote/{{ poll.id }} " method="post"> but I'm not sure how to fix it.
You have your poll id and vote action reversed.
Your url pattern is of the form:
^polls/ ^(?P<poll_id>\d+)/vote/$ [name='vote']
but your form action points to vote/id instead. Reverse those:
<form action="/polls/{{ poll.id }}/vote" method="post">
Note that the tutorial actually uses a different method to generate that URL; it uses:
<form action="{% url 'polls:vote' poll.id %}" method="post">
where the url filter generates the correct URL for you, given the polls:vote route configuration and the id of the current poll object (poll.id).
Using {% url routename arguments %} makes it easier for you to later change your routes, without having to then correct all your templates too.
Change the url to /polls/{{poll.id}}/vote/
in the previous tut03 they have removed the hard-coded part of the url
now it is <form action="{% url 'polls:vote' question.id %}" method="post">
this should work
Related
this is my code
I don't know where the error is coming from. It is telling me that
Using the URLconf defined in ehiz.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
add/ [name='add']
The current path, get, didn't match any of these.
Views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request,'base.html',{'name':'Gael'})
def add(request):
val1 = int(request.GET['num1'])
val2 = int(request.GET['num2'])
res = val1 + val2
return render(request, 'result.html',{'result':res})
base.html
{% extends 'main.html' %}
{% block content %}
<h1>hello {{name}}</h1>
<form action="get">
<input type="text" name="num1" placeholder="Enter first number"><br>
<input type="text" name="num2" placeholder="enter second number"><br>
<input type="submit">
</form>
{% endblock %}
result.html
{% extends 'main.html' %}
{% block content %}
the result is : {{result}}
{% endblock %}
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.home,name='home'),
path('add/',views.add,name='add')
]
In your <form> you are confusing the method="…" with the action="…" attribute. The method="…" specifies the HTTP request type, whereas the action="…" specifies to what endpoint the request will be submitted.
You thus should work with:
<form method="get" action="{% url 'add' %}">
<input type="text" name="num1" placeholder="Enter first number"><be>
<input type="text" name="num2" placeholder="enter second number"><be>
<input type="submit">
</form>
The method="get" is not necessary, since by default a form will be submitted through a GET request.
So i just installed django-registration and got the templates from https://github.com/macdhuibh/django-registration-templates
I'm getting a problem with the URL resolver, I get
Reverse for 'auth_password_reset' with arguments '()' and keyword
arguments '{}' not found.
as well as many others....
urls.py:
#Other stuff,
url(r'^accounts/', include('registration.backends.hmac.urls')),
Exactly as the docs specify.
Furthermore here's the html that throws the error. It's from login.html from the github. It's the one that threw this error but it seems i get something similar every time i try doing a reverse match on a url from the auth_url.py of django-registration.
{% extends "main/header.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
{{ 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 %}
Thanks in advance.
Figured out what went wrong,
I added the URL pattern in the app's urls.py not the root project's one.
Hope this helps someone out too!
I'm currently using django-allauth to manage my registrations and logins.
Until now, all of my logins and signups have worked by having the django-allauth pages at the /accounts/ prefix for url patterns, so to register a new account you would navigate to /accounts/signup/. I want to keep this functionality, but also want to introduce a signup form onto my landing page (at my base url), and I want to have a quick login form in my top banner from anywhere in my website (not /accounts/).
My question is simple, how can we create login and signup forms anywhere throughout the site, without restricting it to a specific prefix of my urls?
If you want to create a signup/login form in your landing page or somewhere else, you can take a look at this question: https://stackoverflow.com/a/24179796/2230003
Basically, for a login/logout form using e-mail only and not username to login, the code in your template would be:
{% load account %}
<h1>Login / Logout</h1>
{% if user.is_authenticated %}
<p>Loged in with e-mail: {{ request.user.email }}</p>
Logout
{% else %}
<form action="{% url "account_login" %}" method="post">
{% csrf_token %}
<input type="email" placeholder="E-mail" name="login">
<input type="password" placeholder="Password" name="password">
<label for="id_remember_menu" class="text-primary">Remember Me:</label>
<input id="id_remember_menu" name="remember" type="checkbox">
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<button type="submit">Login</button>
Forgot Password?
</form>
{% endif %}
I'm a novice to django, altough I have some experience using python. I'm currently learning django, but when I try to use the included login system, I get the following error:
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/usr/lib/python2.7/site-packages/django/contrib/admin/templates/registration/login.html (File does not exist)
/usr/lib/python2.7/site-packages/django/contrib/auth/templates/registration/login.html (File does not exist)
Relevant code from views.py:
#login_required
def userPage(request):
return HttpResponse("Hello User!")
I am using django 1.6.1 and python 2.7.5 on Fedora 20. I have already tried reinstalling django. In the folders from the error message are other templates. How can I resolve this?
You need to provide the urls (project/urls.py)
from django.contrib.auth.views import login
from django.contrib.auth.views import logout
url(
regex=r'^login/$',
view=login,
kwargs={'template_name': 'login.html'},
name='login'
),
url(
regex=r'^logout/$',
view=logout,
kwargs={'next_page': '/'},
name='logout'
),
and the template login.html (something like that)
<form action="{% url 'login' %}" method="post" accept-charset="utf-8">
{% csrf_token %}
{% for field in form %}
<label>{{ field.label }}</label>
{% if field.errors %}
{{ field.errors }}
{% endif %}
{{ field }}
{% endfor %}
<input type="hidden" name="next" value="{{ next }}" />
<input class="button small" type="submit" value="Submit"/>
</form>
Check the documentation about authentication:
It’s your responsibility to provide the html for the login template
The documentation provides a sample template.
How do you load both the Social Login, the Login form and Sign Up form to your Index page using django-allauth?
Something similar like when you go to facebook.com.
accounts/url are already working and I've tried copying the
<form class="login" method="POST" action="{% url 'account_login' %}">
{% csrf_token %}
{{ form.as_p }}
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<a class="button secondaryAction" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
<button class="primaryAction" type="submit">{% trans "Sign In" %}</button>
</form>
to my index.html but it doesn't work.
I'm new to Python Programming and Django development but i've done some excerise with tutorials in the Django Book.
The view that handles your 'index' url and renders your index.html template has to have a login form, for that you have to make it a FormView descendant, and set the class attribute to LoginForm (allauth's login form). Otherwise, you can't render {{form.as_p}} to the template (because there is no form in your context).