0

我有三个模型被分别调用——一个在我的第一列,第二个在我的第二列,第三个在我的第三列。第一个是类别,将保持不变。第二个是帖子,如果选择了一个类别,则需要显示该类别的帖子(如果不是则显示全部),第三个是选定的帖子(如果没有,则不显示)。

我有一些与 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)

网址:

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})
4

1 回答 1

0

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

此外,您应该在模型方法中使用@permalink装饰器。get_absolute_url()这样,如果您应该更改您的网址,您就不会冒链接断开的风险。

模型.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"),
)

视图.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
        }
    )
于 2013-05-04T00:29:41.317 回答