我正试图在我的django博客应用项目中使用slugified url访问页面。
这就是问题模板代码的样子:
{% for post in latest_post_list %}
<li><a href="{% url 'blog:view_post' post.title_text|slugify %}">{{post.title_text}}</a> {{post.pub_date}}</li>
这是我的网址路径:
url(r'^(?P<post_name>)/$', views.view_post, name='view_post'))
这就是我的视图功能:
def view_post(request, post_name):
pdb.set_trace()
try:
post_name = post_name.encode('ascii','ignore') #un-unicode name
unslugified_post_name = post_name.replace('-', ' ') #will get slugified name, replace all hyphens with spaces
post = Post.objects.get(title_text=unslugified_post_name)
except Post.DoesNotExist:
raise Http404("Post does not exist")
return render(request, 'blog/posts.djhtml', {'post': post})
这是我收到的错误的堆栈跟踪:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.7.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Template error:
In template /home/andrew/projects/heroku-blog/My-Django-Blog/blog/templates/blog/index.djhtml, error at line 53
Reverse for 'view_post' with arguments '(u'lorem-ipsum',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'blog/(?P<title_text>)/$']
43 : <h5><a href="/blog/">Programmer Plebeian</a></h5>
44 : </div>
45 : {{navbar|safe}}
46 : </div>
47 : </div>
48 : <div class="row">
49 : <div id="postindex" class="eleven columns">
50 : <ul>
51 : {% if latest_post_list %}
52 : {% for post in latest_post_list %}
53 : <li><a href=" {% url 'blog:view_post' post.title_text|slugify %} ">{{post.title_text}}</a> {{post.pub_date}}</li>
54 : {% endfor %}
55 : </ul>
56 : {% else %}
57 : <p>There is no blog post available here</p>
58 : {% endif %}
59 : </section>
60 : </div>
61 : </div>
62 : </div>
63 :
Traceback:
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/blog/views.py" in index
13. return render(request, 'blog/index.djhtml', context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/shortcuts.py" in render
50. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
178. return t.render(context_instance)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
148. return self._render(context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
142. return self.nodelist.render(context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
80. return node.render(context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
312. return nodelist.render(context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
844. bit = self.render_node(node, context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
80. return node.render(context)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
201. nodelist.append(node.render(context))
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
458. six.reraise(*exc_info)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
444. url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
551. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/andrew/projects/heroku-blog/My-Django-Blog/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
468. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /
Exception Value: Reverse for 'view_post' with arguments '(u'lorem-ipsum',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'blog/(?P<title_text>)/$']
导致此错误的字段的值是“lorem ipsum”
我知道问题可能在路径正则表达式中,但是我无法找出正确的正则表达式,我是新手。我现在所做的就是获取传递给它的字符串,无论它看起来如何。但如果你愿意向我展示一个更强大的,我会非常感激。
另外,有没有办法防止slugify返回unicode字符串?
答案 0 :(得分:3)
view_post
的正则表达式为空。将其更改为:
url(r'^(?P<post_name>[\w-]+)/$', views.view_post, name='view_post')