[Tutor] A Django Beginner View Question

Evert Rol evert.rol at gmail.com
Thu Jul 29 08:57:50 CEST 2010


Consider using the django-users Google group for typical Django questions (unless you figure they're really about Python). Will likely get you better or more answers.
Anyway:

> Hi
> 
> I am building my first Django site which has a lot of effectively 'static' pages where I just want to make the meta data and text editable.

Sounds like you actually want a generic view. Search for the link on the front page of the Django docs. Generic views point your straight from urls.py to the template, bypassing the views.py.
For the second part, you can use the keyword "editable" and set it to False in de model below, for fields you don't want to be edited in the admin (but you'll then have to figure out yourself how to enter the other fields).


> The model is
> 
> models.py
> 
> class About(models.Model):
>    page_title = models.CharField(max_length=900, help_text='Text at top of browser window')
>    meta_keywords = models.CharField(max_length=900, help_text='Keywords (for SEO)')
>    meta_description = models.CharField(max_length=160, help_text='Description (for SEO)')
>    logo_header = models.CharField(max_length=900, help_text='H1 Header (for SEO)')
>    header = models.CharField(max_length=60)
>    body = models.TextField()
>    last_updated = models.DateTimeField(default=datetime.datetime.now, primary_key=True)
> 
>    class Meta:
>        get_latest_by = "last_updated"
>        verbose_name_plural = "About Page"
> #        managed = False
> 
> def __unicode__(self):
>    return self.title
> 
> 
> I'm trying to understand how to either write a model that only allows a single entry or write a view that will take either a single entry or the most recent entry.

Not clear what you want to do: "a model that allows a single entry"? Is that a model which has one, and only one, entry in the database? That doesn't really make much sense. Better do that through your view, as below (again, consider a generic view).


> views.py
> 
> def about(request):
> 	return render_to_response('about.html',
> 								{ 'About' : About.objects.latest() })
> urls.py
> 
>    (r'^about/$', 'harkproject.cms.views.about'),
> 
> Much appreciated
> Al Macmillan



More information about the Tutor mailing list