多个Django模型查询在一个视图中独立更改

时间:2013-05-03 23:57:50

标签: python ajax django django-models django-views

我有三个模型被单独调用 - 一个在我的第一列,第二个在我的第二列,第三个在我的第三列。第一个是类别,并将保持不变。第二个是帖子,如果选择了一个类别,则需要显示这些类别的帖子(如果不是全部显示),第三个是所选帖子(如果没有,则不显示任何内容)。

我有点使用get_absolute_url调用,但是我使用了太多(四个)不同的视图并且它很混乱。此外,这种方式并不总是正常运行。如果我选择包含我的内容的帖子,则帖子列表会更改为该类别,例如您仍然可以显示所有帖子。

如何正常工作?我知道ajax在这方面也会很好,但我想首先在没有ajax的情况下使用它然后实现它,或者只能用ajax实现它?

以下是我的相关代码:

模型:

class Category(models.Model):
    name = models.CharField(max_length=30, unique=True)

    def get_absolute_url(self):
        return "/category/%i/" % self.id

class Post(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=256)
    author = models.CharField(max_length=256)
    link = models.URLField(max_length=512)
    dt_published = models.DateTimeField()
    content = models.TextField()

    def get_absolute_url(self):
        return "/%i/%i/" % (self.category.id, self.id)

URLS:

urlpatterns += patterns('myapp.views',
    url(r'^main/$', 'mainview'),
    url(r'^post/(\d+)/$', 'mainview2'),
    url(r'^category/(\d+)/$', 'mainview3'),
    url(r'^(\d+)/(\d+)/$', 'mainview4'),
)

视图:

def mainview(request):
    category_list = Category.objects.all()
    post_list = Post.objects.all()
    entry = None
    return render(request, 'main.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

def mainview2(request, postid):
    category_list = Category.objects.all()
    post_list = Post.objects.all()
    entry = Post.objects.filter(id=postid)
    return render(request, 'mainview.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

def mainview3(request, catid):
    category_list = Category.objects.all()
    post_list = Post.objects.filter(category=catid)
    entry = None
    return render(request, 'mainview.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

def mainview4(request, catid, postid):
    category_list = Category.objects.all()
    cat_selected = Category.objects.filter(id=catid)
    post_list = Post.objects.filter(category=cat_selected)
    entry = Post.objects.filter(id=postid)
    return render(request, 'main view.html', {'category_list': category_list, 'post_list': post_list, 'entry': entry})

1 个答案:

答案 0 :(得分:0)

您只需一个视图即可解决此问题。

此外,您应该使用模型get_absolute_url()方法中的@permalink decorator。这样,如果您更改网址,则不会冒链接损坏的风险。

<强> models.py

class Category(models.Model):
    name = models.CharField(max_length=30, unique=True)

    @models.permalink
    def get_absolute_url(self):
        return ('browser', (), {'category_id': self.id})

class Post(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=256)
    author = models.CharField(max_length=256)
    link = models.URLField(max_length=512)
    dt_published = models.DateTimeField()
    content = models.TextField()

    @models.permalink
    def get_absolute_url(self):
        return ('browser', (), {'category_id': self.category.id, 'post_id': self.id})

<强> urls.py

urlpatterns = patterns('whatever.views',
    url(r'^browser/$', 'theview', name="browser"),
    url(r'^browser/(?P<category_id>[0-9]+)/$', 'theview', name="browser"),
    url(r'^browser/(?P<category_id>[0-9]+)/(?P<post_id>[0-9]+)/$', 'theview', name="browser"),
)

<强> views.py

from django.shortcuts import get_object_or_404, render

def theview(request, category_id=None, post_id=None):
    categories = Category.objects.all()
    posts = None
    selected_cat = None
    selected_post = None
    if category_id:
        selected_cat = get_object_or_404(Category, pk=category_id)
        posts = selected_cat.post_set.all()
        if post_id:
            selected_post = posts.filter(pk=post_id)

    return render(
        request,
        'template.html',
        {
            'categories': categories,
            'selected_cat': selected_cat,
            'posts': posts,
            'selected_post': selected_post
        }
    )