New to Django framework. Mostly reading through documentations. But this one i am unable to crack.
Trying to add a URL to an headline, that will be forwarded to the 'headlines' post.
The Error:
NoReverseMatch at / Reverse for 'assignment_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['assignment_detail/'] Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 2.0.2 Exception Type: NoReverseMatch Exception Value: Reverse for 'assignment_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['assignment_detail/'] Exception Location: C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\lib\\site-packages\\django\\urls\\resolvers.py in _reverse_with_prefix, line 632 Python Executable: C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\Scripts\\python.exe Python Version: 3.6.2 Python Path:
['C:\\Users\\internit\\Dropbox\\Python\\codepython\\codepython', 'C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\Scripts\\python36.zip', 'C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\DLLs', 'C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\lib', 'C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\Scripts', 'c:\\program files (x86)\\python36-32\\Lib', 'c:\\program files (x86)\\python36-32\\DLLs', 'C:\\Users\\internit\\Dropbox\\Python\\codepython\\env', 'C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\lib\\site-packages'] Server time: Thu, 8 Feb 2018 14:53:07 +0000 Error during template rendering In template C:\\Users\\internit\\Dropbox\\Python\\codepython\\codepython\\codepython\\templates\\base.html, error at line 0Reverse for 'assignment_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['assignment_detail/'] 1 {% load static %} 2 3 4 5 6 7 8 9 10 CODEPYTHON.NET Traceback Switch to copy-and-paste view C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\lib\\site-packages\\django\\core\\handlers\\exception.py in inner response = get_response(request) ... ▶ Local vars C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\lib\\site-packages\\django\\core\\handlers\\base.py in _get_response response = self.process_exception_by_middleware(e, request) ... ▶ Local vars C:\\Users\\internit\\Dropbox\\Python\\codepython\\env\\lib\\site-packages\\django\\core\\handlers\\base.py in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ... ▶ Local vars C:\\Users\\internit\\Dropbox\\Python\\codepython\\codepython\\home\\views.py in home return render(request, 'home.html', {'post':post}) ... ▶ Local vars
home/urls.py
from django.conf.urls import url
from django.conf import settings
from django.conf.urls.static import static
from codepython.posts import views
from posts import views as ps
app_name ='home'
urlpatterns = [
url(r'^$/', views.create, name='create'),
url(r'(?P<pk>\d+)/$', views.home, name='home'),
url(r'(?P<pk>\d+)/$', views.userposts, name='userposts')
url(r'^posts/(?P<post_id>[0-9]+)/$', ps.assignment_detail, name='assignment_detail'),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
home/views.py
from django.shortcuts import render, get_object_or_404
from django.apps import apps
# Create your views here.
def home(request):
posts = apps.get_model("posts", "Post")
post = posts.objects.all().order_by('-pub_date')[0:6]
return render(request, 'home.html', {'post':post})
def assignment_detail(request, post_id):
posts = apps.get_model('posts', 'Post')
post = get_object_or_404(posts, pk=post_id)
return render(request, "assignment_detail.html", {'post': post})
home.html
<div class="row">
{% for post in post.all %}
<div class="col-md-4">
<div class="thumbnail">
<div class="caption">
<p>Level: {{post.assignment_level}}</p>
<a href="{% url 'assignment_detail' post_id %}"><h3>{{ post.title }}</h3></a>
<p>by {{post.author}} from {{post.pub_date}}</p>
<h4>{{post.assignment_body}}</h4>
<p><a href="#" class="btn btn-primary" role="button">Read...</a></p>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock%}
myproject/urls.py
url(r'^assignment_detail/', views.assignment_detail,name='assignment_detail'),
What am I missing here. Thank you in advance.
Your url does not imply that you have to pass an id, but you're passing one in the template:
<a href="{% url 'assignment_detail' post_id %}"><h3>{{ post.title }}</h3></a>
It should be:
url(r'^assignment_detail/(?P<post_id>[0-9]+)', views.assignment_detail,name='assignment_detail'),
That error is Django telling you that it can't find any URLs named 'assignment_detail' that have an argument to pass in.
This is because your url entry in myproject/urls.py
is missing the argument ( post_id
) that you use in your view. You'll need to update that url line to something similar to this:
url(r'^assignment_detail/(?P<post_id>[0-9]+)/$', views.assignment_detail, name='assignment_detail'),
The change at the end of the URL adds a named regular expression to capture the post_id
value which will then be passed into the view.
Looking at your template code, you'll need to update your {% url %} block to use post.id
(notice period) not post_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.