TemplateDoesNotExist at /poll/1/ -Django tutorial - python

I got an error, TemplateDoesNotExist at /polls/1/
I'm supposed to see the result of the vote after I vote.
Error is:
polls/details.html
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/1/
Django Version: 3.0.8
Exception Type: TemplateDoesNotExist
Exception Value:
polls/details.html
Exception Location: C:\Users\JohnDoe\Documents\JaneDoe\Django\trydjango\tutorial\lib\site-packages\django\template\loader.py in get_template, line 19
Python Executable: C:\Users\JohnDoe\Documents\JaneDoe\Django\trydjango\tutorial\Scripts\python.exe
Python Version: 3.8.4
detail.html
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.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>
Templates are nested in a polls folder which is in a templates folder in the polls app.
I don't understand why I am getting an error in details.
How can I fix this?

Its a typo in your code. You need to change template_name="polls/details.html" to template_name="polls/detail.html" in your View.
Or, just rename the polls/detail.html file to polls/details.html.

Related

NoReverseMatch at - django polls app tutorial

I keep getting this when trying to load da page:
NoReverseMatch at /polls/
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']
In template C:\Users\sarah\Desktop\django2\myproject\my_site\polls\templates\polls\index.html, error at line 20
line 20:
<form action="{% url 'polls:vote' question.id %}" method="post">
I am a complete beginner at django, css, html, ... I kept checking in with the tutorial, comparing my code with the code reference in the tutorial, however I see no mistake.
My index.html:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.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>
The vote function in views.py:
def vote(request, question_id):
question = get_object_or_404(Question, pk=question_id)
try:
selected_choice = question.choice_set.get(pk=request.POST['choice'])
except (KeyError, Choice.DoesNotExist):
# Redisplay the question voting form.
return render(request, 'polls/detail.html', {
'question': question,
'error_message': "You didn't select a choice.",
})
else:
selected_choice.votes += 1
selected_choice.save()
return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))
urls.py:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
Thank you.
The main reason here is you are using your index.html with the form where you should really have this in polls/detail where you have access to a question object by pk. Right now you are trying to access the id property of question but question is undefined in your index.html.
Place the following in your polls/details.html
<h1>{{ question.question_text }}</h1>
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.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>
Then, navigate to polls/1 or whatever question.id you want and then try and use the form.

Django LoginView keeps displaying form error messages on refresh

Using Django's built-in LoginView, I want to display an error message on incorrect login. This works fine. However, upon refreshing of the login page, the error message persists.
I want to remove this error message when the user refreshes the page.
urls.py
from django.urls import path
from django.contrib.auth.views import LoginView
urlpatterns = [
path('login/', LoginView.as_view(template_name='login.html'), name="login"),
]
login.html
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
{% for error in form.non_field_errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}
<form method="post" action="{% url 'accounts:login' %}">
{% csrf_token %}
<p><input type="text" name="username" autofocus="" required="" id="id_username" placeholder="username"></p>
<p><input type="password" name="password" required="" id="id_password" placeholder="password"></p>
<button type="submit" value="Login">Login</button>
<a id="signup-btn" href="{% url 'accounts:signup' %}"">Don't have an account? Sign up</a>
</form>
The error message being printed:
'Please enter a correct username and password. Note that both fields may be case-sensitive.'
Any help would be greatly appreciated.
Edit:
Sorry for any confusion, I have updated the question to clarify things.
I used your own code, what I did was add a button with the data-dispats attribute to close the bootstrap alert when required, the complete element looked like this:
{% if form.non_field_errors %}
<div class="alert alert-danger" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
{% for error in form.non_field_errors %}
<p>{{ error }}</p>
{% endfor %}
</div>
{% endif %}

error during django template rendering

working in django tutorials and this error in template rendering appears
NoReverseMatch at /music/1/ Reverse for 'favorite' with arguments
'(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
this is a detailed view for the problem
Error during template rendering
In template E:\Codes\bucky - Django Tutorials for Beginners\website\music\templates\music\detail.html, error at line 10
Reverse for 'favorite' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
1 <img src="{{ album.album_logo }}">
2
3 <h1>{{ album.album_title }}</h1>
4 <h2>{{ album.artist }}</h2>
5
6 {% if error_message %}
7 <p><strong>{{ error_message }}</strong></p>
8 {% endif %}
9
10 <form action="{% url 'music:favorite' album.id %}" method="post">
11 {% csrf_token %}
this is my music/urls.py
app_name = 'music'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<album_id>[0-9]+)/$', views.detail ,name='detail'),
url(r'^(?P<album_id>[0-9]+)/favorite/$', views.favorite ,name='favorite'),
]
the detail.html file where the error appear in the form
<img src="{{ album.album_logo }}">
<h1>{{ album.album_title }}</h1>
<h2>{{ album.artist }}</h2>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form action="{% url 'music:favorite' album.id %}" method="post">
{% csrf_token %}
{% for song in album.song_set.all %}
<input type="radio" id="song{{ forloop.counter }}" name="song" value="{{ song.id }}">
<label for="song{{ forloop.counter }}">
{{ song.song_title }}
{% if song.is_favorite %}
<img src="http://i.imgur.com/b9b13Rd.png">
{% endif %}
</label>
<br>
{% endfor %}
<input type="submit" value="Favorite">
</form>

Include urls for django-registration

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!

NoReverseMatch at /polls/1/vote/ when following Django's official tutorial

I'm following the Django tutorial. I've reached part 4, at the moment of building the vote form. Sadly, I cannot find the problem that is causing the following error:
NoReverseMatch at /polls/1/vote/
Reverse for 'vote' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/(?P[0-9]+)/vote/$']
Both /polls/1/vote and /polls/1/ throw the error above.
My polls/urls.py
from django.conf.urls import url
from . import views
app_name = 'polls'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>[0-9]+)/$', views.details, name='details'),
url(r'^(?P<question_id>[0-9]+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
And here's the details.html, which shows the error at line 11, the form opening tag:
<h1>{{ question.question }}</h1>
{% if error %}
<p>
<strong>
{{ error }}
</strong>
</p>
{% endif %}
<form action="{% url 'polls:vote' question_id %}" method="POST">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}"/>
<label for="choice{{ forloop.counter }}">{{ choice.choice }}</label>
<br/>
{% endfor %}
<input type="submit" value="Vote">
</form>

Categories

Resources