开发者

Django seems to be caching?

开发者 https://www.devze.com 2023-03-20 12:23 出处:网络
I\'m running Django 1.2.1 for my personal website with a blog. It\'s all fine and dandy, but I\'ve found that all the browsers I\'ve tried (Firefox, Chromium, Opera) are caching webpages, which of cou

I'm running Django 1.2.1 for my personal website with a blog. It's all fine and dandy, but I've found that all the browsers I've tried (Firefox, Chromium, Opera) are caching webpages, which of course is a problem for other users viewing my blog (being that it won't load new posts up unless they empty their cache or force refresh the page). I didn't have this problem when my site ran on PHP, so how would I go about fixing this seemingly Django-related problem?

I've only been working with Django for about a week or so, so I don't really know where abouts I should be looking to fix something like this. Thanks in advance!

The way I do each page (and each blog post) is as a Page/Post object respectively so I can use the admin interface without having to write my own. Although the issue is happening for both situations, I'll just give the Post class for now:

class Post(models.Model):
    author = models.ForeignKey(User, default=User.objects.get(username='nathan'))
    status = models.ForeignKey(Status, default=Status.objects.get(text='Draft'))
    date = models.DateTimeField(default=datetime.datetime.now())
    title = models.CharField(max_length=100)
    post = models.TextField()
    categories = models.ManyToManyField(Category)

    def __unicode__(self):
        return u'%s' % self.title

    def link(self):
        return u'/blog/post/%s' % self.title

    class Meta:
        ordering = ['-date']

And here's my urls.py:

from django.conf.urls.defaults import *                                                                       
from django.views.generic import list_detail

from feeds import PostFeed

from models import Post

blog_posts = { 
    'queryset': Post.objects.filter(status__text__exact='Published'),
}

urlpatterns = patterns('getoffmalawn.blog.views',
    (r'^$', list_detail.object_list, blog_posts),
    (r'^archive/(\d{4})$',开发者_运维百科 'archive'),
    (r'^rss/$', PostFeed()),
    (r'^search/$', 'search'),
    (r'^tag/(.+)/$', 'tag'),
    (r'^post/(.+)/$', 'post'),
)

If you guys would like to see the code from views.py, just ask and I'll throw that up too.

Edit:

Here's the views.

view.py for the blog App:

from django.http import HttpResponse, HttpResponseRedirect                                                                              
from django.shortcuts import render_to_response, redirect
from django.core.context_processors import csrf
from django.db.models import Q

from models import Post, Category

def post(request, title):
    post = Post.objects.get(title=title)
    c = locals()
    c.update(csrf(request))
    return render_to_response('blog/post_detail.html', c)

def blog_detail(request, blog_id):
    post = get_object_or_404(Post, pk=blog_id)

    return list_detail.object_detail(request, queryset=Post.objects.all(), object_id=blog_id)

def archive(request, month, year):
    pass

def search(request):
    if request.method == 'GET':
        query = request.GET['q']

        object_list = Post.objects.filter(Q(post__icontains=query) | Q(title__icontains=query), status__text__exact='Published')

        return render_to_response('blog/post_list_sparse.html', locals())

def tag(request, tag):
    object_list = Post.objects.filter(categories__text__exact=tag, status__text__exact='Published')
    return render_to_response('blog/post_list.html', locals())


The problem was that it really was browser-based caching, as I was told by a member on the Django-users mailing list. The reason I didn't see this problem in PHP is that it was sending cache suppression headers.

The solution was to add the @never_cache decorator to the relevant views.


Here is the documentation for caching in django.

https://docs.djangoproject.com/en/1.2/topics/cache/

Not ideal, but you can make all of the pages tell the browsers not to cache anything, to see if that helps.

https://docs.djangoproject.com/en/1.2/topics/cache/#controlling-cache-using-other-headers

0

精彩评论

暂无评论...
验证码 换一张
取 消