以前,你们给我那个"使用搜索工具"那种言论。是的,我已经做到了,但无济于事。无论如何,过去两天我一直在摸不着头脑,想弄清楚为什么我一直得到这个错误"反向' post_share'有参数'('',)'未找到。尝试了2种模式:['博客\ /(?P [0-9] +)\ /分享\ / $','博客\ / post_share \ / $&#39 ]"
我正在使用Django 2,而我正在读安东尼奥·梅尔(Antonio Mele)的“代码为Django的示例”一书。
class EmailPostForm(forms.Form):
name = forms.CharField(max_length = 30)
email = forms.EmailField()
to = forms.EmailField()
comments = forms.CharField(required = False, widget = forms.Textarea)
def post_share(request, post_id):
post = get_object_or_404(Post, id = post_id, status = 'published')
sent = False
if request.method == 'POST':
form = EmailPostForm(request, POST)
if form.is_valid():
cd = form.cleaned_data
post_url = request.build_absolute_uri(post.get_absolute_url())
subject = '{} ({}) recommends you reading "{}"'.format(cd['name'], cd['email'], post.title)
message = 'Read "{}" at {}\n\n{}\'s comments: {}'.format(post.title, post_url, cd['name'], cd['comments'])
send_mail(subject, message, 'sample@gmail.com', [cd['to']])
sent = True
else:
form = EmailPostForm()
return render(request, 'blogg/shared.html', {
'post': post,
'form': form,
'sent':sent
})
app_name = 'blog'
urlpatterns = [
path('post_list', views.post_list, name = 'post_list'),
path('post_detail/<int:year>/<int:month>/<int:day>/<str:post>', views.post_detail, name = 'post_detail'),
path('post_share/', views.post_share, name = 'post_share'),
# re_path(r'^(?P<post_id>\d+)/share/$', views.post_share, name='post_share'),
path('<int:post_id>/share/', views.post_share, name='post_share'),
]
{% extends 'blogg/base.html'%}
{% block title %} {{ post.title }} {% endblock %}
{% block content %}
<h1> {{ post.title }}</h1>
<p>Published {{ post.publish }} by {{ post.author }}</p>
{{ post.body|linebreaks }}
<p>
<a href="{% url 'blog:post_share' post.id %}"> Share Post </a>
</p>
{% endblock %}
NoReverseMatch at /blog/post_detail/2018/04/24/simple-test
Reverse for 'post_share' with arguments '('',)' not found. 2 pattern(s) tried: ['blog\\/(?P<post_id>[0-9]+)\\/share\\/$', 'blog\\/post_share\\/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/post_detail/2018/04/24/simple-test
Django Version: 2.0.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'post_share' with arguments '('',)' not found. 2 pattern(s) tried: ['blog\\/(?P<post_id>[0-9]+)\\/share\\/$', 'blog\\/post_share\\/$']
Exception Location: C:\Dev\blog\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 632
Python Executable: C:\Dev\blog\Scripts\python.exe
Python Version: 3.6.4
Python Path:
['C:\\Dev\\blog\\src\\mysite',
'C:\\Dev\\blog\\Scripts\\python36.zip',
'C:\\Dev\\blog\\DLLs',
'C:\\Dev\\blog\\lib',
'C:\\Dev\\blog\\Scripts',
'c:\\program files (x86)\\python36-32\\Lib',
'c:\\program files (x86)\\python36-32\\DLLs',
'C:\\Dev\\blog',
'C:\\Dev\\blog\\lib\\site-packages']
Server time: Tue, 24 Apr 2018 12:40:59 +0000`
答案 0 :(得分:0)
尝试在函数开头声明runnable
和to
变量,就像这样..
name