Django: how can I generate a unique URL for the news?

Learn Django. In General, the task model, but still good options in head do not go. There is a website, there is news, we have to somehow generate a unique URL for each of them. Presumably this can be done based on the date + some unique identifier, but can not decide how it should work: forming the URL somehow with a system of templates, or to store for each news in the base of your URL and substitute it into the template. Also still not decided how to provide this very "uniqueness".

Somebody tell me its normal decision — implementation tasks? Well or again MB you can take the source code of a blog on Django to dig?
October 8th 19 at 03:09
6 answers
October 8th 19 at 03:11
Solution
I did so (to turn 20120221-1 — date-id):
url.py —
url(r'^news/(?P(\d){8})?(\-)?(?P(\d)+)?$', news_views.show_news, name="news-url")<br> models.py:<br> <code>class News(models.Model):<br> ...........<br> def get_absolute_url(self):<br> return "/news/%s" % (self.news_date.strftime('%Y%m%d') + '-' + str(self.id))</code><br> template.html:<br> <code> {{ news.title }}</code>
before went. In General the model has a get_absolute_url method for this. All can be found in the documentation docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url - Gennaro.Ebert commented on October 8th 19 at 03:14
Thank you :) - Collin_Mills commented on October 8th 19 at 03:17
October 8th 19 at 03:13
If you like CNC and the idea of the breadcrumbs, the preferable scheme /year/month/day/id-slug? for DetailView. The user can always watch the news for a certain period, ListView, simply by modifying the url in the browser, quite a logical way.
October 8th 19 at 03:15
I have something like this

class Post(models.Model):
 title = models.CharField(max_length=64, verbose_name='post Title')
 uid = models.CharField(max_length=64, unique=True, blank=True, verbose_name='Url')
...........
 def get_absolute_url(self):
 return '/blog/post/%s.html' % self.uid

 def save(self, *args, **kwargs):
 if not self.uid:
 lastid = Post.objects.latest('id')
 self.uid = str(lastid.id + 1 ) +'-'+ createUid(self.title)
 super(Post, self).save(*args, **kwargs)


in the file functions.py
import trans

def createUid(uid):
 return uid.encode('trans').replace(' ', '_').lower();


The url comes out like this
/blog/post/149-eroticheskie_gifki.html
October 8th 19 at 03:17
Model.SlugField, django.template.defaultfilters.slugify
October 8th 19 at 03:19
I understand you're interested in that slug based on the title may not be unique and it should be affectionate (if it already is). The first thing that came to hand — bitbucket.org/neithere/django-autoslug

In General, — www.google.com/search?hl=ru&q=django+autoslug
October 8th 19 at 03:21
I just generated a uuid

Find more questions by tags Django