I am getting this error for going to the url : http://127.0.0.1:9000/mypolls/anyerror
NoReverseMatch at /mypolls/anyerror
Reverse for 'mypolls' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['mypolls/(?P<error_message>.*)$']
My urls.py contains:
from django.conf.urls import url
from . import views
urlpatterns=[
url(r'^$',views.home,name='home'),
url(r'^polls/$',views.list,name='list'),
url(r'^polls/(?P<pk>[0-9]+)$',views.detail,name='detail'),
url(r'^polls/(?P<pk>[0-9]+)/vote$',views.vote,name='vote'),
url(r'^mypolls/(?P<error_message>.*)$',views.mypolls,name='mypolls'),
url(r'^create/$',views.create,name='create'),
url(r'^polls/(?P<pk>[0-9]+)/edit$',views.edit,name='edit'),
url(r'^polls/(?P<pk>[0-9]+)/delete$',views.delete,name='delete'),
]
My views.py contains:
def mypolls(request,error_message=None):
polls=Poll.objects.filter(author=request.user)
return render(request,'polls/list.html',{'polls':polls,'error_message':error_message})
What is wrong here?
My list.html contains:
{% extends 'polls/base.html' %}
{% block content %}
{% if error_message %}
<p>{{error_message}}</p>
{% endif %}
{% for poll in polls %}
<li><a href='{% url "detail" pk=poll.pk %}'>{{ poll.question }}</a>
{% endfor %}
{% endblock %}
The problem was in base.html. I did not give the keyword argument in url for mypolls in base.html. Now I have set the url as:
{% url "mypolls" error_message="" %}
and it is working fine.
Thanks #Alasdair and everyone.
Related
when I am trying to grab items from db.html with the help of id it is showing an error
I cant understand where is the problem
please help me out
venue.html
{% extends 'MYapp/index.html' %}
{% block content %}
<center>
<h1> venue.html </h1>
<br>
<div class="card-header">
Featured
</div>
<div class="card-body container-fluid">
<h5 class="card-title">Special title treatment</h5>
{% for venues in venue_list %}
<p class="card-text container-fluid">
{{ venues }} {{ venues.lastname}}
{% endfor %}
</p>
</div>
</center>
{% endblock %}
views.py
from django.shortcuts import render
from django.http import *
from MYapp.models import *
from .form import *
def index(request):
return render(request,'MYapp/index.html')
def venue(request):
venue_list = Task.objects.all()
return render(request,'MYapp/venue.html',{'venue_list': venue_list})
def db(request, db_id):
all = Task.objects.get(pk= db_id)
return render(request,'MYapp/db.html',{'all': all})
urls.py
another error occers hear
it is showing page is not found
because of this path('db/<db_id>/', views.db, name ='db'),
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('nature', views.nature, name ='nature'),
path('', views.index, name ='index'),
path('footer', views.footer, name ='footer'),
path('navebar', views.navebar, name ='navebar'),
path('form', views.form, name ='form'),
path('venue', views.venue, name ='venue'),
path('db/<db_id>/', views.db, name ='db'),
]
When you render the template venue.html (in the view function called venue()), you are not passing the variable all to the context, you are just passing venue_list.
That causes the error because in the template in the part href="{% url 'db' all.id %}", the variable all will not be defined.
I found that error because of putting wrong url in path
<p class="card-text container-fluid">
<a href="{% url 'db' all.id %}"> {{ venues }} {{
venues.lastname}}</a>
{% endfor %}
</p>
change the name of variable to venues because u did it in for loop
then
<a href="{% url 'db' venues.id %}"> {{ venues }} {{
venues.lastname}}</a>
{% endfor %}
</p>
I would like to link this function to the link in the base Templates, but I have this error, what solution could be found? Should I use the Reverse function?
my views
def forumPostList(request, pk):
conversation = get_object_or_404(Conversation, pk=pk)
form_response = PostModelForm()
posts_conversation = Post.objects.filter(conversation=conversation)
context = {"conversation": conversation,
"posts_conversation": posts_conversation,
"form_response": form_response
}
return render(request, "account/forum_post.html", context)
{% extends 'base.html' %}
{% block content %}
<h1>Received messages:</h1>
<hr>
<br>
{% for post in posts_conversation %}
<h3>{{ conversation.title }}</h3>
<p>Posts: {{ post.author_post.posts.count }}</p>
<p>Posts: {{ post.author_post.username }}</p>
{% endfor %}
{% endblock content %}
My base
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{% url 'forum_post' %}">forum_post</a>
</li>
forumPostList view needs to get 'pk' as parameter in url. So for calling it with a link, your link should specify 'pk'.
So you can use url block like this:
{% url 'forum_post' conversation_pk %}
That coversation_pk should be specified somehow.
I'm currently working through the Django tutorial and just finished part 3, but am getting this error when trying to access http://127.0.0.1:8000/polls/:
TemplateSyntaxError at /polls/
Invalid block tag on line 27: '<span', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Request Method: GET
Request URL: http://127.0.0.1:8000/polls/
Django Version: 3.1.7
Exception Type: TemplateSyntaxError
Exception Value:
Invalid block tag on line 27: '<span', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
Error during template rendering
I think there's a problem with my views.py file, but I've copy/pasted everything from the tutorial trying to avoid these issues. This is the view that the error is being thrown on:
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
I'm very new to this so if I'm missing something obvious, please point it out.
index.html:
{% 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 %}
I think your problem lie here : <a href="{% url ‘polls:detail' question.id %}">, more precisely in ‘polls:detail'
Notice how the first quote is not the same character as the second one. You probably meant :
<a href="{% url 'polls:detail' question.id %}">`
In the index.html file try adding {% endfor %} in new line, I think this should fix.
{% 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 %}
I don't know why I am getting a 404 error on the page flights/1 when clearly, I have a url pattern for
<int:flight_id> (flights is a app). Help!
My urls.py for the project
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('flights/', include('flights.urls'))
]
My urls.py for the flights app
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<int:flight_id>", views.flight, name="flight"),\
]
And the views.flight
def flight(request, flight_id):
flight = Flight.objects.get(id=flight_id)
return render(request, "flights/flight.html", {
"flight": flight,
"passengers": flight.passengers.all(),
"non_passengers": Passenger.objects.exclude(flights=flight).all()
})
Template
{% extends "flights/layout.html" %}
{% block body %}
<h1>Flight {{ flight.id }}</h1>
<ul>
<li>Origin: {{ flight.origin }}</li>
<li>Destination: {{ flight.destination }}</li>
<li>Duration: {{ flight.duration }}</li>
</ul>
<h2>Passengers</h2>
<ul>
{% for passenger in passengers %}
<li>{{ passenger }}</li>
{% empty %}
<li>No passengers.</li>
{% endfor%}
</ul>
<h2>Add Passenger</h2>
<form method="POST" action="{% url 'flight' flight.id book %}">
{% csrf_token %}
<select name="passengers">
{% for passenger in non_passengers %}
<option value="{{ passenger.id }}">{{ passenger }}</option>
{% endfor %}
</select>
</form>
Back to Flight List
{% endblock %}
With layout.html being html boilerplate code
I'm new to Django and faced with next problem: when I turn on the appropriate link I get next error:
NoReverseMatch at /tutorial/
Reverse for 'tutorial.views.section_tutorial' with arguments '(1L,)' and keyword arguments '{}' not found.
What am I doing wrong? and why in the args are passed "1L" instead of "1"? (when i return "1" i get same error.) I tried to change 'tutorial.views.section_tutorial' for 'section-detail' in my template but still nothing has changed. Used django 1.5.4, python 2.7; Thanks!
tutorial/view.py:
def get_xhtml(s_url):
...
return result
def section_tutorial(request, section_id):
sections = Section.objects.all()
subsections = Subsection.objects.all()
s_url = Section.objects.get(id=section_id).content
result = get_xhtml(s_url)
return render(request, 'tutorial/section.html', {'sections': sections,
'subsections': subsections,
'result': result})
tutorial/urls.py:
from django.conf.urls import patterns, url
import views
urlpatterns = patterns('',
url(r'^$', views.main_tutorial, name='tutorial'),
url(r'^(?P<section_id>\d+)/$', views.section_tutorial, name='section-detail'),
url(r'^(?P<section_id>\d+)/(?P<subsection_id>\d+)/$', views.subsection_tutorial, name='subsection-detail'),
)
urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^tutorial/$', include('apps.tutorial.urls')),
)
main.html:
{% extends "index.html" %}
{% block content %}
<div class="span2" data-spy="affix">
<ul id="menu">
{% for section in sections %}
<li>
{{ section.name }}
<ul>
{% for subsection in subsections%}
{% if subsection.section == section.id %}
<li><a href=#>{{ subsection.name }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</li>
</ul>
</div>
<div class="span9">
<div class="well">
{% autoescape off%}
{{ result }}
{% endautoescape %}
</div>
</div>
{% endblock %}
You don't need $ identifier in url regex in your main urls file when including app urls:
url(r'^tutorial/$', include('apps.tutorial.urls')),
should be:
url(r'^tutorial/', include('apps.tutorial.urls')),