Displaying both slug and ID in URL, but route by ID only in Django -


What I'm trying to achieve: My News app should display a slug, but only the query of the article by ID Unfortunately, I want to get the code (reverse for '')

,) 'And keyword logic' {} 'were not found. When attempting to browse an article, the URL generated in the template looks correct as mentioned above (I can confirm it by searching a search through the hestack, which gives the correct URL).

models.py

  class news (models.model): id = models.IntegerField (primary_key = true, editable = false) Category = models.CharField (MAX_LENGTH = 50L) title = models.CharField (MAX_LENGTH = 200L) rss_summary = models.TextField (MAX_LENGTH = 2000L) body_text = models.TextField (MAX_LENGTH = 5000L) POST_DATE = models.DateTimeField () prettyurl = models .SlugField (MAX_LENGTH = 100 L) Square Meta: db_table = 'news' def __unicode __ (self): return self.title def get_absolute_url (auto): urlresolvers.reverse ('news_detail' kwargs = {'pk': self.id , 'Slug': self.prettyurl}) Return   

urls.py urlpatterns = Pattern ('', url (R '^ $', visible = view. NewsListView.as _view), name = 'news_index', url (r '? ^ (P & lt; p & gt; \ d +) /' ?, see = views.NewsDetailView.as_view (), name = 'news_detail'), URL (R '^ (P & LT; P & gt; \ d +?) / (P & LT; Slag & gt; [- \ W]) / $', see = views.NewsDetailView.as_view (), name = 'News_detail'), URL (R '^ Collection / $', view = views. NewsArchiveIndexView.as_view (), name = "archive_month"), [... more unrelated URLs ...]

views.py < Pre> class NewsDetailView (DetailView): # name = 'news_detail'), model = news context_object_name = 'news' #slug_url_kwarg = 'prettyurl' #slug_field = 'prettyurl' TEMPLATE_NAME = 'news / detail.html'

Template

  `& lt; P & gt; & Lt; A href = "{% url 'news_detail' news.slug%}" & gt; Permalink & lt; / A & gt; For this article. Thanks Daniel, Rosenman and @UV, with your help, I have defined my problem with URL patterns. By managing it:  
  r '(? P & lt; pk & gt; \ d +) (?: / (? P  [  
  • News / NN
  • News / NN
  • News / NN /
  • News / NN / A- Slag
  • News / NN / A-Slug /

    In the template, I

      {% url 'news_detail' news.id News.prettyurl%}   

    which is the fourth number in the list listed above. Shows Krn.

    Thanks again!

  • Comments

    Popular posts from this blog

    c - Mpirun hangs when mpi send and recieve is put in a loop -

    python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

    java - Unable to get JDBC connection in Spring application to MySQL -