How To Use The Category Name As Url Suffix In Django
Now my url is like:https://mywebsite.com/newscategory_lists-5 I want change it to:https://mywebsite.com/categoryname Here is my model: class Category(models.Model): name = mode
Solution 1:
try this, in url.py
url(r'^(?P<link>[\w|-]+)/$', views.categoryNewsList, name='categoryNewsList')
view.py
def categoryNewsList(request, link):
categories = {
"graphics-design": "GD",
"digital-marketing": "DM",
"video-animation": "VA",
"music-audio": "MA",
"programming-tech": "PT"
}
try:
foos = Category.objects.filter(name=categories[link])
return render(request, 'yoursite/home.html', locals())
except KeyError:
return redirect('yoursite/home.html')
and at your template.html put links:
<nav class="navbar navbar-expand-sm bg-light navbar-light justify-content-center">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/">All categories</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/graphics-design">Graphics & Design</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/digital-marketing">Digital Marketing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/video-animation">Video & Animation</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/music-audio">Music & Audio</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/programming-tech">Programming & Tech</a>
</li>
</ul>
Post a Comment for "How To Use The Category Name As Url Suffix In Django"