I am creating little blog app. Here is a snippet from my views: views.py
from django.shortcuts import render_to_response
from blog.post.models import Post, Comment
from django.contrib.auth.models import User
from django.http import Http404
PAGE_SIZE = 5
ABSTRACT_CONTENT_SIZE = 300
def main(request, page = 0):
post_objects = Post.objects.filter(visible = True)[page : page + PAGE_SIZE]
posts = [开发者_如何学Go]
for p in post_objects:
if len(p.content) > ABSTRACT_CONTENT_SIZE:
abstract = p.content[:ABSTRACT_CONTENT_SIZE] + '...'
else:
abstract = p.content
posts.append({
'subject': p.subject,
'content': abstract,
'author': p.author,
'date': p.date,
'noc': p.number_of_comments,
'number': p.pk
})
return render_to_response('main.html',{'posts': posts})
I would like to put those 2 constants into database (so I could later manage them from admin). The question is, how should I load them in views, or maybe there is another way to perform this task? Thank you in advance.
class SiteSettings(models.Model):
value = models.IntegerField() # Assuming you are only using integer value
type = models.CharField(unique=True) # could also make this the primary key
PAGE_SIZE = SiteSettings.Objects.filter(type="page_size").get()
# you should be able to do this as well since type is unique
PAGE_SIZE = SiteSettings.Objects.get(type="page_size")
PAGE_SIZE.value
Check all this configuration apps to store site-wise info in your databse
精彩评论