Reputation: 333
I have a blog app, this is the relevant part of views.py, of course belonging to the blog app.
from django.conf import settings
from django.shortcuts import render
from .models import Blog
# Create your views here.
def view_homepage(request):
return render(request, 'index.html', {})
def view_aboutpage(request):
return render(request, 'about.html', {})
def view_blogpost(request, blog_id):
article = Blog.objects.get(pk=blog_id)
return render(request, 'damn.html', {'article':article})
this is the urls.py in blog app
from django.conf.urls import url, include, patterns
from blog import views
urlpatterns = [
url(r'^$', views.view_homepage, name=''),
url(r'about/$', views.view_aboutpage, name='about'),
url(r'blog/(?P<blog_id>\d+)/$', views.view_blogpost, name='post'),
]
This is the regular urls.py in the project.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('blog.urls')),
]
This is the error, I am having.
NoReverseMatch at /
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<blog_id>\\d+)/$']
Request Method: GET
Request URL: http://localhost:8000/
Django Version: 1.9
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<blog_id>\\d+)/$']
Exception Location: C:\Users\filip\Python\ENV\lib\site- packages\django\core\urlresolvers.py in _reverse_with_prefix, line 508
Python Executable: C:\Users\filip\Python\ENV\Scripts\python.exe
Python Version: 3.5.1
This is the html:
<li>
<a href="{% url "" %}">Home</a>
</li>
<li>
<a href="{% url "about" %}">About</a>
</li>
<li>
<a href="{% url "post" article.id %}">Sample Post</a>
</li>
Reverse for 'post' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P\d+)/$']
Upvotes: 0
Views: 1693
Reputation: 599450
Like the error says, you don't have a URL called "post" that takes no arguments; the URL you do have called that is expecting a blog_id argument. So, you should pass that in your tag; since you have a context variable called article
it would be:
<a href="{% url "post" article.id %}">Sample Post</a>
Upvotes: 4