2016-10-04 59 views
0
類別

正確重定向:Django的 - 基於我有兩個問題

編輯:

我做了一些調整和部分地解決了問題。但現在,當我提交新的博客文章表單時,它會將我帶到錯誤的網址,無論我嘗試使用哪個網址,都找不到完整的文章。當我點擊鏈接到完整的帖子時,我得到一個類型錯誤「對象」後「不可迭代」。類別「progresstracker」完美的作品,「blogtopics/..」沒有。

以下是最新代碼:

views.py

def pt_detail(request, slug, category): 
    post = get_object_or_404(Post, slug=slug, category__slug=category) 

    template = CATEGORY_TEMPLATES.get(post.category.slug) 
    return render(request, template, {'post': post}) 


def bt_detail(request, slug, category): 
    post = get_object_or_404(Post, slug=slug, category__slug=category) 
    template = CATEGORY_TEMPLATES.get(post.category.slug) 
    return render(request, template, {'post': post}) 

def post_new(request): 
    if request.method == "POST": 
     form = PostForm(request.POST) 
     if form.is_valid(): 
      post = form.save(commit=False) 
      post.author = request.user 
      post.published_date = timezone.now() 
      post.save() 
      if post.category.slug == 'blog-topics': 
       return redirect('bt_detail', slug=post.slug, category=post.category) 
      else: 
       return redirect('pt_detail', slug=post.slug, category=post.category) 
    else: 
     form = PostForm() 
    return render(request, 'blog/post_edit.html', {'form': form}) 

def post_edit(request, slug, category): 
    post = get_object_or_404(Post, slug=slug, category__slug=category) 
    if request.method == "POST": 
     form = PostForm(request.POST, instance=post) 
     if form.is_valid(): 
      post = form.save(commit=False) 
      post.author = request.user 
      post.published_date = timezone.now() 
      post.save() 
      if post.category.slug == 'blog-topics': 
       return redirect('bt_detail', slug=post.slug, category=post.category) 
      else: 
       return redirect('pt_detail', slug=post.slug, category=post.category) 
    else: 
     form = PostForm(instance=post) 
    return render(request, 'blog/post_edit.html', {'form': form}) 

urls.py

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^home/$', views.home, name='home'), 
    url(r'^blogtopics/computer-science/$', views.compsci, name='computer-science'), 
    url(r'^blogtopics/data-science/$', views.datasci, name='data-science'), 
    url(r'^blogtopics/other/$', views.other, name='other'), 
    url(r'^blogtopics/$', views.blogtopics, name='blogtopics'), 
    url(r'^resources/$', views.resources, name='resources'), 
    url(r'^new/$', views.post_new, name='post_new'), 
    url(r'^progresstracker/$', views.progresstracker, name='progresstracker'), 
    url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/$', views.bt_detail, name='bt_detail'), 
    url(r'^blogtopics/(?P<category>[\w-]+)/(?P<slug>[\w-]+)/edit/$', views.post_edit, name='post_edit'), 
    url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/$', views.pt_detail, name='pt_detail'), 
    url(r'^(?P<category>[\w-]+)/(?P<slug>[\w-]+)/edit/$', views.post_edit, name='post_edit'), 
] 

computerscience.html

{% extends 'blog/base.html' %} 

{% block nav %} 
    <li><a href="/home">Home</a></li> 
    <li><a href="/progresstracker">Progress Tracker</a></li> 
    <li class="dropdown"> 
     <a class="active" href="/blogtopics" class="dropbtn">Blog Topics</a> 
     <div class="dropdown-content"> 
      <a href="/blogtopics/computer-science">Computer Science</a> 
      <a href="/blogtopics/data-science">Data Science</a> 
      <a href="/blogtopics/other">Other</a> 
     </div> 
    </li> 
    <li><a href="/resources">Resources</a></li> 
{% endblock %} 


{% block content %} 
    <div id="content"> 
       <div class="padding"> 
       <p style="padding-top: 50px"> 
       <div class="post_list"> 
      {% for post in post %} 
       <h3><a href="{{ post.get_absolute_url_bt }}">{{ post.title }}</a></h3> 
       <p>{{ post.published_date }}</p> 
      {% endfor %} 
       </div> 
       </div> 
      </div> 
{% endblock %} 

models.py

class Post(models.Model): 
    title = models.CharField(max_length=100, unique=True) 
    slug = models.SlugField(max_length=100, unique=True) 
    body = models.TextField() 
    posted = models.DateField(db_index=True, auto_now_add=True) 
    category = models.ForeignKey('books.Category', related_name='%(class)s_slug') 
    created_date = models.DateTimeField(
     default = timezone.now) 
    published_date = models.DateTimeField(
     blank=True, null=True) 

    def publish(self): 
     self.published_date = timezone.now() 
     self.save() 

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

    def get_absolute_url(self): 
     return "/%s/%s/" % (self.category, self.slug) 

    def get_absolute_url_bt(self): 
     return "/blogtopics/%s/%s/" % (self.category, self.slug) 

回答

0

看起來你有幾件事情在這裏發生。首先你說任何類別都會被髮送到一個空白的電腦科學頁面。這告訴我,您的if語句總是導致錯誤,因此它試圖呈現computerscience.html模板。 pt_detail視圖在上下文中有一個Post對象(ptpost)。 compsci視圖使用相同的computerscience.html模板在上下文中使用posts呈現Post對象的QuerySet。你不能這樣做。由於您的模板在您的上下文中預計爲posts,並且只能找到ptpost,因此它無法呈現任何內容,因此是空白頁面。

至於使用類別來決定使用,你可以使用一系列elif語句,模板,或者你可以做這樣的事情:

CATEGORY_TEMPLATES = { 
    'progresstracker': 'blog/pt_detail.html', 
    'anothercategory': 'blog/ac_detail.html', 
    'yetanothercategory': 'blog/yac_detail.html', 
} 

def pt_detail(request, slug, category): 
    ptpost = get_object_or_404(Post, slug=slug, category__slug=category) 
    template = CATEGORY_TEMPLATES.get(ptpost.category.slug, 'blog/default_detail.html') 
    return render(request, template, {'ptpost': ptpost}) 

這將從CATEGORY_TEMPLATES詞典選擇類別,如果當前類別不在字典中,則使用blog/default_detail.html作爲默認值。

+0

感謝您的迴應!我做了一些調整(參見原文帖子編輯),一半修正了問題,但仍然掙扎。感謝任何幫助。 – Andrew