Home > other >  Django wrong url?
Django wrong url?

Time:10-17

I have two urls.One is for blogs and the other is for games.I have app for games and blogs.

path('',include('games.urls')),
   path('blog',include('blogs.urls')),

My gameapp url is like this:

path('<slug:platform>/<slug:slug>',views.oyun,name='detail'),

My blogapp urls are like this:

path('', views.blogs, name='blog'),
    path('/<slug:slug>', views.blog_id, name='blog_id'),

This url is going to(path('/<slug:slug>', views.blog_id, name='blog_id')), views.oyun so it is giving error. How can i solve this?

Views.py

def blogs(request):
    blogss=blog.objects.all().order_by('-created')
    return render(request,"blog.html",{"blogs":blogss})


def blog_id(request,slug):

    blog_id=blog.objects.get(seo_url=slug)



    return render(request,"blog-writings.html",{"blog_id":blog_id})

def oyun(request,platform,slug):
   oyun = Oyunlar.objects.get(slugyap=slug)


...

CodePudding user response:

The detail path will capture the URLs that are supposed to fire the blog_id path. Indeed, in the games.urls we see:

path('<slug:platform>/<slug:slug>',views.oyun,name='detail'),

whereas for the blog_id, the complete URL path is:

     'blog/<slug:slug>'

This thus means that if you visit blog/foo, it will fire the oyun view with 'blog' as value for the platform variable, and 'foo' as value for the slug variable.

You can swap the paths, such that it will first inspect the items with a blog/ prefix, and only will fire the oyun view, if that is not the case:

urlpatterns = [
    path('blog',include('blogs.urls')),  # ← first the blog/ paths.
    path('',include('games.urls')),
]
  • Related