正確重定向: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)
感謝您的迴應!我做了一些調整(參見原文帖子編輯),一半修正了問題,但仍然掙扎。感謝任何幫助。 – Andrew