Im working on a blog, i'm trying to create a comments link from each blog post, which will go a page where the individual post with comments can be seen.
In my "list.html", where all the blog posts are listed, i have this code for each blog post:
<div class="blogpost-comments">Comments</div>
This then submits to my URLconf shown below:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
# ex: /post/5/ pk=5
url(r'^post/(?P<pk>\d+)/$', views.post, name='post'),
)
And below my view is shown:
def post(request, pk):
post = Post.objects.get(pk=pk)
return render(request, 'blog/post.html', {'post': post, 'user': request.user})
Im wanting to have the URL structure:
/blog/post/{pk}/
To access individual posts. For some reason i'm currently getting the error:
NoReverseMatch at /blog
Reverse for 'post' with arguments '(6L,)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog$post/(?P<pk>\\d+)/$']
Which i don't understand. Can anybody help?
EDIT:
the main urls.py is as follow:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^blog/$', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Remove the $ from
url(r'^blog/$', include('blog.urls')),
so that it is
url(r'^blog/', include('blog.urls')),
You aren't actually including your blog urls because the regex is stopping at the $
Related
I am new to Django and I was working at my first project, but I got:
NoReverseMatch with Exception Value: 'hiring_log_app' is not a
registered namespace in base.html
which follows:
href="{% url 'hiring_log_app:index' %}" >Hiring Log
href="{% url 'hiring_log_app:topics' %}" >Topics
I made sure my namespace was correct and I looked at the other topics already opened without figuring out how to solve the issue.
I paste urls.py:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include ('hiring_log_app.urls', namespace='hiring_log_app')),
hiring_log_app/urls.py:
"""Define URL patterns for hiring_log_app"""
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^topics/$', views.topics, name='topics'),
]
views.py:
from django.shortcuts import render
from .models import Topic
def index(request):
"""The home page for hiring_log_app"""
return render(request, 'hiring_log_app/index.html')
def topics(request):
""" list of topics"""
topics = Topic.objects.raw( "SELECT text FROM HIRING_LOG_APP_TOPIC;")
context = {'topics' : topics}
return render(request, 'hiring_log_app/topics.html', context)
Does anyone know where I am making a mistake?
You have wrongly specified the namespace in urlpatterns. Follow the pattern below:
from django.contrib import admin
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))
Edit:
If you are using Django 2.2.4 then you should use path instead of url since the usage of it is replaced by re_path.
from django.contrib import admin
from django.conf.urls import include
from django.urls import re_path
urlpatterns = [
re_path(r'^admin/', include(admin.site.urls)),
re_path(r'', include (('hiring_log_app.urls','hiring_log_app'), namespace='hiring_log_app'))
I'm trying to run a blog build with django on the browser. And I got this error:
NoReverseMatch at /
Reverse for 'blog.views.post_detail' not found.
'blog.views.post_detail' is not a valid view function or pattern name.
My url.py of my app looks like:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^post/(?P<pk>[0-9]+)/$', views.post_detail),
]
It seems that when I type 127.0.0.1:8000/.
The url will direct to views.post_list.
And my views.py looks like:
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.objects.filter(published_date__isnull=False)
return render(request, 'blog/post_list.html', {'posts': posts}
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
post_list() will render the request with post_list.html.
Inside post_list.html, the error comes from the line:
<h1>{{ post.title }}</h1>
I don't really understand what 'Reverse' means in the error message. 'blog.views.post_detail' does exist in views.py. I think I got everything needed for the code and can't figure out what went wrong.
I'm new to django, sorry if the question is basic and thanks for answering!
Django 1.10 removed the ability to reverse urls by the view's dotted import path. Instead, you need to name your url pattern and use that name to reverse the url:
urlpatterns = [
url(r'^$', views.post_list, name='post-list'),
url(r'^(?P<pk>\d+)/$', views.post_detail, name='post-detail'),
]
And in your template:
<h1>{{ post.title }}</h1>
It seems that your urls.py should be as follows:
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^$', views.post_list),
url(r'^(?P<pk>\d+)/$', views.post_detail),
]
You should define a name for your url:
urlpatterns [
url(r'^$', views.post_list,name=post_list),
]
then use url tag like this:
AppName is you django application name.
I'm newbie in Django and i read many topic here and not found solution for my case. I believe it's easy, but i can't find the solution.
Basically i have the code in my urls.py and the works fine in Django 1.8.4:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'controli2c.view.view_home'),
]
My template file contains
{% url 'controli2c.views.view_home' as home_url%}
<a href="{% url 'controli2c.views.view_home' %}" {% if request.path == home_url %} class="active"{% endif %} >HOME</a>
When i update Django, i get the error "TypeError: view must be a callable or a list/tuple in the case of include()". Then, i change my urls.py code to:
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'views.view_home'),
]
Now, i have the NoReverseMatch when i open the browser (http://localhost:8000):
"Reverse for 'controli2c.view.view_home' not found. 'controli2c.views.view_home' is not a valid view function or pattern name."
In a post in the forum i found:
"The solution is to update your urls.py to include the view callable. This means that you have to import the view in your urls.py. If your URL patterns don't have names, then now is a good time to add one, because reversing with the dotted python path no longer works."
I believe that's my problem. But i don't know what changes i have to do.
Anyone can help me?
Thanks a lot!!
Now you have to pass a callable, so:
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', views.view_home),
]
I think it might work now.
I found the solution!
To keep my template file with the same code, i have make these change
from django.conf.urls import include, url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^leds/', include('ledscontrol.urls')),
url(r'^', 'views.view_home',name='controli2c.views.view_home'),
]
Thanks!
Before this does not happened but after installing django-contrib-comments app, when I click on a post detail link to get post I get 404 error. But in shell there is no problem
my urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<type>\w+)/(?P<slug>[\w|\W]+)/$', views.included_posts,
name="included_posts"),
url(r'^post/(?P<slug>[\w|\W]+)/$', views.detail, name="detail"),
url(r'^paginated-tags/$', views.listing, name="listing"),
)
my views.py:
def detail(request, slug):
posts = Post.published_posts.all()
post = get_object_or_404(posts, slug=slug)
return render(request, 'blog/index.html', {'post': post})
published_posts is my custom manager.
Your included_posts url catches the post/someslug url before the detail url.
Move the included_posts at the end of the urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^post/(?P<slug>[\w|\W]+)/$', views.detail, name="detail"),
url(r'^paginated-tags/$', views.listing, name="listing"),
url(r'^(?P<type>\w+)/(?P<slug>[\w|\W]+)/$', views.included_posts,
name="included_posts"),
)
As side note: ([\w-]+) is the common regex for slugs. Your ([\w|\W]+) will match any string (for example: "some () nonslug [] chars")
Trying to make django-registration work within the Django Tutorial polls projects.
I'm using Django 1.6, django-registration 1.0 and the django-registration-templates
When I try to access
http://localhost:8000/accounts/login/
I get
NoReverseMatch at /accounts/login/
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
The line in the template, mysite/templates/base.html, that's cited in the error report is :
{% trans "Home" %} |
And I do have a url with name 'index' in my polls.url :
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
So I feel like that should work ? Help ?
EDIT 1
polls.urls:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^(?P<pk>\d+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>\d+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
mysite.urls:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('registration.backends.default.urls')),
)
registration.urls:
"""
Backwards-compatible URLconf for existing django-registration
installs; this allows the standard ``include('registration.urls')`` to
continue working, but that usage is deprecated and will be removed for
django-registration 1.0. For new installs, use
``include('registration.backends.default.urls')``.
"""
import warnings
warnings.warn("include('registration.urls') is deprecated; use include('registration.backends.default.urls') instead.",
DeprecationWarning)
from registration.backends.default.urls import *
OK I've figured out the problem here. The url with name 'index' was defined in polls.urls and so within the template I needed to change :
{% trans "Home" %}
to
{% trans "Home" %}
As mentioned in the Django 1.6 documention of the url tag and which I quote here for convenience :
If you’d like to retrieve a namespaced URL, specify the fully
qualified name:
{% url 'myapp:view-name' %}
This will follow the normal namespaced URL resolution strategy,
including using any hints provided by the context as to the current
application.