2๐
โ
Use two fileds in your models, say;
class Post(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
and while saving yor Post, you shoud save slug also;
In views;
from django.template.defaultfilters import slugify
post = Post.objects.create(title=title, slug=slugify(title))
this slug will be unique for each post.
In urls.py
url(r'^(?P<slug>[\w-]+)/$', 'myapp.views.post_detail', name='post_detail'),
In views.py
def post_detail(request, slug):
x = Post.objects.get(slug=slug)
๐คGeo Jacob
0๐
in models.py:
title = models.SlugField(unique=True)
in urls.py:
urlpatterns = [
...
url(r'^(?P<title>\d+)/$', views.article_view, name = 'article_detail'),
...
]
in views.py:
def article_view(request, title):
...
article = get_object_or_404(Article, title=title)
...
in template.html:
<a href="{% url 'article_detail' title=article.title %}">
๐คBalas
- [Answered ]-Trying to run django-static-sitemaps in terminal using "django-admin refresh_sitemap" gives error of "Unknown command: 'refresh_sitemap''
- [Answered ]-Deploying django project on apache
- [Answered ]-Django: Not allowing newly-created user to log in until he/she is approved by the admin?
Source:stackexchange.com