我在向网址添加额外变量时遇到了一些麻烦。我目前将其设置为:
url(r'^(?P<blog_id>[0-9]+)/?$', 'blog.views.blog_show', name='show'),
但是我想在标题中加入slug:
url(r'^(?P<blog_id>[0-9]+)/(?P<blog_slug>\S+)/?$', 'blog.views.blog_show', name='show'),
我设法让它工作但是当我的用户在我的博客上更新评论时,会发生一些有趣的事情,因为它没有记录到数据库并且没有正确地重定向。
urls.py
urlpatterns = [
# Examples: /blogs
url(r'^$', 'blog.views.index', name='index'),
url(r'^new$', 'blog.views.new', name='new'),
url(r'^create$', 'blog.views.create', name='create'),
url(r'^(?P<blog_id>[0-9]+)/?$', 'blog.views.blog_show', name='show'),
url(r'^(?P<blog_id>[0-9]+)/edit$', 'blog.views.blog_edit', name='edit'),
url(r'^(?P<blog_id>[0-9]+)/update$', 'blog.views.blog_update', name='update'),
url(r'^(?P<blog_id>[0-9]+)/delete$', 'blog.views.delete_blog', name='delete'),
url(r'^(?P<blog_id>[0-9]+)/new_comment$', 'blog.views.new_comment', name='comment'),
# url(r'^blog/', include('blog.urls')),
]
views.py
def new_comment(request, blog_id, blog_slug):
#blog = Blog.objects.get(pk=blog_id)
blog = get_object_or_404(Blog, pk=blog_id)
c = Comment()
c.comment_content = request.POST['comment_content']
c.blog_id = blog
c.user_id = User.objects.all()[0]
c.save()
return HttpResponseRedirect(reverse('show', args=(blog.id,)))
def blog_show(request, blog_id, blog_slug):
#blog = Blog.objects.get(pk=blog_id)
blog = get_object_or_404(Blog, pk=blog_id)
comment_obj = blog.comment_set.all().order_by('-id')
paginator = Paginator(comment_obj, 2)
page = request.GET.get('page')
try:
comment_list = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
comment_list = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
comment_list = paginator.page(paginator.num_pages)
template_hash = {'comment_list':comment_list,'blog':blog}
return render(request, 'blog/show.html',template_hash)
show.html
{% load django_markdown %}
<h1>{{ blog.blog_title }}</h1>
<p>
{{ blog.blog_content|markdown}}
</p>
<form action = '/blogs/{{blog.id}}/{{blog.slug}}/new_comment' method = "post">{% csrf_token %}
<div>New Comment:<br><textarea type = "text" name ='comment_content'></textarea></div>
<input type = "submit" value = "Submit">
</form>
<h3>Comments</h3>
{% if comment_list %}
{% for comment in comment_list %}
<p>{{comment}}</p>
{% endfor %}
{% else %}
<p>No Comments</p>
{% endif %}
<div class="pagination">
<span class="step-links">
{% if comment_list.has_previous %}
<a href="?page={{ comment_list.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ comment_list.number }} of {{ comment_list.paginator.num_pages }}.
</span>
{% if comment_list.has_next %}
<a href="?page={{ comment_list.next_page_number }}">next</a>
{% endif %}
</span>
</div>
我遇到的问题是当我将其重定向并记录到数据库时,评论将不再记录,重定向将停留在http://localhost:8000/blogs/1/my-first-blog/new_comment。而不只是去http://localhost:8000/blogs/1/my-first-blog/
我虽然通过更新以下内容会正确重定向,但它没有
<form action = '/blogs/{{blog.id}}/{{blog.slug}}/new_comment' method = "post">{% csrf_token %}
任何帮助都会非常感激。
答案 0 :(得分:1)
问题是\S+
匹配所有字符串字符,包括&#34; /&#34;。因此,正则表达式'^(?P<blog_id>[0-9]+)/(?P<blog_slug>\S+)/?$
匹配&#34; 1 / my_blog / new_comment&#34;,您的表单最终会提交到展示视图而不是添加评论视图。
使用\w+
来匹配字母数字字符。