How to write urlpatterns in django? - python

I have this structure of urls:
page/section/subsection/article, where section, subsection and article are user-generated slug names.
How can I write the urlpatterns?
I do this, but may be exist better method?
urlpatterns = [
url(r'^$', views.index),
url(r'^(?P<slug>[-\w]+)/$', views.section),
url(r'^(?P<slug>[-\w]+)/(?P<subslug>[-\w]+)/$', views.subsection),
url(r'^(?P<slug>[-\w]+)/(?P<subslug>[-\w]+)/(?P<articleslug>[-\w]+)/$', views.article)
]
My views:
def index(request):
return render(request, 'MotherBeeApp/index.html', {})
def section(request, slug):
sections = Section.objects.filter(page=slug)
if sections:
return render(request, 'MotherBeeApp/section.html', {'Sections': sections})
else:
return render(request, 'MotherBeeApp/404.html', status=404)
def subsection(request, slug, subslug):
subsection = Section.objects.get(link_title=subslug)
articles = Article.objects.filter(section=subsection.pk)
page_title = subsection.title
return render(request, 'MotherBeeApp/subsection.html', {'Articles': articles, 'PageTitle': page_title})
def article(request, slug, subslug, articleslug):
article = Article.objects.get(link_title=articleslug)
return render(request, 'MotherBeeApp/article.html', {'Article': article})

If you are using Django version older than Django 2.0 (< 2.0) than you are doing right thing and you are already using optimistic way. but if your Django version is later than or equals to Django 2.0 you could write urlpatterns as shown here.

Maybe you can upgrade your Django to 2.0+, and then use code as follows:
from django.urls import path, include
urlpatterns = [
path('', views.index),
path('<slug:slug>/', include([
path('', views.section),
path('<slug:subslug>/', views.subsection),
path('<slug:subslug>/<articleslug>/', views.article),
])),
]

Related

NoReverseMatch at /blog/redirect2

I'm new to django and I've been trying to test the URLs redirection methods I've tried two options but the return render() keeps giving me this error of no reverse match
this is my urls.py code :
app_name = 'blog'
urlpatterns = [
# path('about', views.about, name = 'about'),
path('about.html', views.about, name='about'),
path('home.html', views.home, name='home'),
# path('home', views.home, name ="home")
path('redirect', views.redirect_view),
path('redirection_fct',views.redirection_fct, name='redir_fct'),
#redirection par la fonction as_view
path('redirect1', RedirectView.as_view(url='http://127.0.0.1:8000/blog/redirection_fct')),
path('redirect2', views.redirect_view1),
]
and this is my views file :
def about(request):
return render(request, 'about.html', {})
def home(request):
return render(request, 'home.html', {})
#redirection par HttpResponseRedirect
def redirect_view(request):
return HttpResponseRedirect("redirection_fct")
def redirect_view1(request):
return redirect('redir_fct')
def redirection_fct(request):
return render(request, 'redirection.html', {})
Is there something wrong with my URLs pattern or it is the render one?
Is this the urls.py on your app? How do you include it on your project urls.py, if you have a namespace for it, then you probably need something like:
def redirect_view1(request):
return redirect('blog:redir_fct')
You can try running from a django shell:
from django.urls import get_resolver
print(get_resolver().reverse_dict.keys())

Django base.models.Post.DoesNotExist

I am very new to django. I have followed the steps from a video on youtube on how to create a simple blog. Basically, I have a portfolio main page that displays several things including the latest blogs. It all works well. I can see the latest posts on the main page. I can also click in each of them and they open up fine.
However, now, I want to create a new page (blog.html) that will hold all the posts. I created a blog.html and did the necessary settings in views.py and urls.py. However, for some reason I get an error when trying to access localhost/blog/
This is my urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('work/', views.work, name='work'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
path('blog/', views.blog, name='blog'),
]
Here views.py:
def home(request):
posts = Post.objects.all()
return render(request, 'home.html', {'posts': posts})
def post_detail(request, slug):
post = Post.objects.get(slug=slug)
return render(request, 'post_detail.html', {'post': post})
def blog(request):
posts = Post.objects.all()
return render(request, 'blog.html', {'posts': posts})
This happens if you trigger the post_detail view with a slug, and no Post with that slug exists. But here you simply trigger the wrong view. This is because <slug:slug>/ will also match blog/, so you will never trigger the blog/ view. You should reorder the views, so:
urlpatterns = [
path('', views.home, name='home'),
path('work/', views.work, name='work'),
# before <slug:slug>
path('blog/', views.blog, name='blog'),
path('<slug:slug>/', views.post_detail, name='post_detail'),
]
It might however be better to make non-overlapping patterns, since now if you ever make a Post with blog as slug, that post is inacessible. You thus might want to rewrite the path to:
urlpatterns = [
path('', views.home, name='home'),
path('work/', views.work, name='work'),
path('blog/', views.blog, name='blog'),
# non-overlapping patterns
path('post/<slug:slug>/', views.post_detail, name='post_detail'),
]
Note: It is often better to use get_object_or_404(…) [Django-doc],
then to use .get(…) [Django-doc] directly. In case the object does not exists,
for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using
.get(…) will result in a HTTP 500 Server Error.

CreateView weird behaviour. Goes to the wrong view?

In my urls.py I have a ListView and a CreateView. When I have both the views in the urls patterns, the CreateView shows the html linked to the ListView. But when I remove the ListView from the url patterns, then the CreateView shows the correct html.
urls.py
If I have it like this, CreateView shows ListView html
urlpatterns = [
path("", views.TopicListView.as_view(), name="topic"),
path("<topic>/", views.PostListView.as_view(), name="post"),
path("create/", views.CreatePostView.as_view(), name="create_post")
]
This way, the CreateView behaves like I want it to. Shows the correct HTML
urlpatterns = [
path("", views.TopicListView.as_view(), name="topic"),
path("create/", views.CreatePostView.as_view(), name="create_post")
]
views.py
class PostListView(ListView):
model = models.ForumPost
template_name = "forum/post_list.html"
def get_context_data(self):
context = super().get_context_data()
return context
def get_queryset(self):
query_set = super().get_queryset()
return query_set
class CreatePostView(CreateView):
model = models.ForumPost
fields = ("title", "description")
template_name = "forum/create_post.html"
The reason as urls are checking one by one from up to down. So when you have 3 urls:
urlpatterns = [
path("", views.TopicListView.as_view(), name="topic"),
path("<topic>/", views.PostListView.as_view(), name="post"),
path("create/", views.CreatePostView.as_view(), name="create_post")
]
And try to use create/ it actually matches <topic>/ pattern as a string were passed. So what I recommend is to place it to very down:
urlpatterns = [
path("", views.TopicListView.as_view(), name="topic"),
path("create/", views.CreatePostView.as_view(), name="create_post")
path("<topic>/", views.PostListView.as_view(), name="post"),
]
But also it would be better if you add some additional path to urls in order it won't be double-minded like list/<topic>/.

Unable to retrieve a single item when through HTTPRequest with Django Rest

I follow the instructions on Django Restframework tutorial here and has configured everything after it, but somehow, I can't use id to retrieve the json for a single item.
I expect to get a json representation of a single item when i request localhost:8080/items/[index], but instead of getting the item with the corresponding index I would get json representation of every items in I have.
My views.py
#api_view(['GET'])
def item_list(request):
"""
List and create items
"""
if request.method == 'GET':
items = Item.objects.all()
serializer = ItemSerializer(items, many=True)
return Response({'data': serializer.data })
#api_view(['GET'])
def item_detail(request, pk):
"""
Retrieve an item
"""
try:
item = Item.objects.get(pk=pk)
except Item.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = ItemSerializer(item)
return Response(serializer.data)
My urls.py in item module
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'$', views.item_list),
url(r'^items/(?P<pk>[0-9]+)/$', views.item_detail),
]
My urls.py in app module
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^', include('menu.urls')),
]
Edit: Thanks for the reply but it I think I have found a fix. It seems like by putting 'url(r'$', views.item_list)' in front all links will reroute to item_list(). The easy fix is just put it behind 'url(r'^items/(?P[0-9]+)/$', views.item_detail)'
switch the url paths in your url.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^items/(?P<pk>[0-9]+)/$', views.item_detail),
url(r'^$', views.item_list),
]
and include like
urlpatterns = [
path('admin/', admin.site.urls),
url(r'', include('menu.urls')),
]

Trouble with namespace. Two different Urls are rendering the same view

I am having trouble with my urls. I have one app called users that has two models, Salon and Stylist. The url /stylists and /salons is rendering the same view (stylist_results) however /salons should render salon_results and I cannot figure out why. I think there may be something wrong with the way I am using namespaces.
users/urls.py
from django.conf.urls import url
#import views from the current directory
from . import views
urlpatterns=[
url(r'^$', views.stylist_results, name='stylist_results'),
url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
url(r'^$', views.salon_results, name='salon_results'),
url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]
users/views.py
from django.shortcuts import get_object_or_404, render
from .models import Salon, Stylist
# Create your views here.
def salon_results(request):
salons = Salon.objects.all()
return render(request, 'salons/salon_results.html', {'salons': salons})
def salon_detail(request, pk):
salon = get_object_or_404(Salon, pk=pk)
return render(request, 'salons/salon_detail.html', {'salon': salon})
def stylist_results(request):
stylists = Stylist.objects.all()
return render(request, 'stylists/stylist_results.html', {'stylists': stylists})
def stylist_detail(request, pk):
stylist = get_object_or_404(Stylist, pk=pk)
return render(request, 'stylists/stylist_detail.html', {'stylist': stylist})
urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
url(r'^salons/', include('users.urls', namespace='salons')),
url(r'^stylists/', include('users.urls', namespace='stylists')),
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
]
urlpatterns += staticfiles_urlpatterns()
views.py
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
Split your stylist and salon views into two separate modules. You might consider creating two different apps, but you don't have to.
users/stylist_urls.py
urlpatterns = [
url(r'^$', views.stylist_results, name='stylist_results'),
url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
]
users/salon_urls.py
urlpatterns = [
url(r'^$', views.salon_results, name='salon_results'),
url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]
Then update your project's urls.py with the new modules:
urlpatterns = [
url(r'^salons/', include('users.salon_urls', namespace='salons')),
url(r'^stylists/', include('users.stylist_urls', namespace='stylists')),
...
]
At the moment, the regexes for your salon URLs are exactly the same as the stylist URLs, so the salon URLs will always match first.
You are specifying same set of url's(users.urls) for salons and stylists here:
url(r'^salons/', include('users.urls', namespace='salons')),
url(r'^stylists/', include('users.urls', namespace='stylists')),
What did you expect to happen
You are including same users/urls.py in urls.py
It does following:
find me /stylist/ => go into included urls
find first occurance of url(r'^$', views.stylist_results, name='stylist_results'),
renders that view
same thing happens with /salons/
URL Dispatcher documentation

Categories