Hi,
This code will make sure that you first get the first tutorial category (you can only have one with each slug). If found it will show the category page (the same code you have, except that you already have the category). If the category is not found, it will check if there is a tutorial with the slug. If there isn't anything the code then raises a Http404 exception - which means that the item cannot be found.
The reason you are having problems is that the last item in your URLs is a catch all - it will get all items that aren't found previously. So if you want to go to admin, you need to add the admin urls BEFORE your catchall.
Also - the way that you are handling getting the categories or tutorials isn't really good.
You should do something like this:
category = TutorialCategory.objects.filter(category_slug=single_slug).first()
if category:
# Show the category
tutorial = Tutorial.objects.filter(tutorial_slug = single_slug).first()
if tutorial:
# Show the tutorial
raise Http404
Regards,
Andréas
Den mån 29 juli 2019 kl 14:48 skrev Marsel Beqiri <marselbeqiri.365@gmail.com>:
Hi, i;m new here, also in django developing,--Lastly i tried to create an app, something like a blog. And I have created a Model with FK relations like this: Category=>Subcategory=>Post.
Till here everything is fine, also when I create a Post in Admin Panel there is a char field to put the post url ex. post-nr-1
But I want to make my app URL, friendly, just Url no '/ ' (single slug) . like thisclick on category: main/category-title-1click on post: main/post-title-1So just one URL, not slashing.
urlpatterns = [path("", views.homepage, name = "homepage"),path("register/",views.register, name = "register"),path("logout/", views.logout_request, name = "logout"),path("login/", views.login_request, name = "login"),path("<slug:single_slug>/", views.single_slug, name="single_slug"),]View function mentioned before
def single_slug(request, single_slug): # Mund te pranojme single_slug si variabel neper URL path# Me poshte kemi nje comprehensice Listcategories = [c.category_slug for c in TutorialCategory.objects.all()] # c.category_slug : do marri vetem URL (slug) dhe do i ruaj ne liste. Pra shmang te dhena qe sna duhen# c.category_slug => Bene te mudnur qe gjat ciklit for te zgjighen vetem category_slug ne objekteif single_slug in categories:matching_series = TutorialSeries.objects.filter(tutorial_category__category_slug=single_slug)series_urls = {}for m in matching_series.all():part_one = Tutorial.objects.filter(tutorial_series__tutorial_series=m.tutorial_series).earliest("tutorial_published")series_urls[m] = part_one.tutorial_slugreturn render(request,"main/category.html",{"part_ones": series_urls})tutorials = [t.tutorial_slug for t in Tutorial.objects.all()]if single_slug in tutorials:this_tutorial = Tutorial.objects.get(tutorial_slug = single_slug)return render(request,"main/tutorial.html",{"tutorial": this_tutorial})return HttpResponse(f"{single_slug} does not correspond to anything!")
Now the posts and the category response ok, but if i try to go at /admin or /login or /logout I get the response ex. admin does not correspond to anything!
Even Even if I type a random url, it shows {single_slug} does not correspond to anything!I think You understand me, waiting for a reply pleaseImages:
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/61ea7af0-bc85-4238-ac3c-2c767dcaeca6%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCccu3V_jhhzUtS_dAzHMXf6nn2qq22JE3vhXQJuTqZm%3Dw%40mail.gmail.com.
No comments:
Post a Comment