Sunday, October 5, 2014

Re: Failure to get data from database and onto template.

The only thing I can think of is to delete 3 and 4 and then re-create them and hope that puts them in the right order. Any other ideas?
That should work just fine, as long as you only add at the end in the future. If it becomes an issue more, you may want to consider adding an "order" or "ranking" column so you have more control.
 
I was wrong about the Articles and Sections on the same view. Turns out it was the ID that was controlling everything, Django wasn't even looking at the slug, even when I put unique_together on the meta. Changing the names of the Sections didn't even work. Neither did putting article_id in the Sections regex. If I put sectionView first in the urlconf, all the sections would come up fine but none of the articles. If I put articleView first in the urlconf, the articles all came up and the sections got doesnotexist. The only thing that worked (and I tried A LOT of variations) was to put the Sections on (?P<slug>) only, no id, no digits.
I think what you are looking for is something like this:

 url(r'^(?P<id>\d+)\/(?P<slug>[-\w]+)', views.article_or_section_view, name='article_or_section_view'),


def article_or_section_view(request, **kwargs):
   
try:
        article
= Article.objects.get(pk=id, slug=slug)
       
return render(request, 'statute2.html', {'article': article)
   
except Article.DoesNotExist:
       
pass
   
try:
        section
= Section.objects.get(pk=id, slug=slug)
       
return render(request, 'statute2.html', {'section': section)
   
except Section.DoesNotExist:
       
pass
   
raise Http404('no article or section matches that id and slug')


Though Django's unique_together only applies to that one model. If you post roughly what your models are we could give more pointers.

I personally have found it helpful to re-use the same model using something like this:
class Content(models.Model):
    type
= models.CharField(max_length=30, choices=[('article', 'Article'), ('section', 'Section')])
    title
= models.CharField(max_length=255)
    slug
= models.CharField(max_length=255)
    content
= models.TextField()
    parent
= models.ForeignKey('self', blank=True, null=True, help_text='example: article of a section')



    

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/980c0223-d42d-427a-a8c4-cd1258e4a463%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment