反向&#39; post_share&#39;有参数&#39;(&#39;&#39;,)&#39;未找到。尝试了2种模式:[&#39; blog \\ /(?P <post_id> [0-9] +)\\ / share \\ / $&#39;,&#39; blog \\ / post_share \\ / $&#39;]

时间:2018-04-24 12:27:48

标签: django

以前,你们给我那个&#34;使用搜索工具&#34;那种言论。是的,我已经做到了,但无济于事。无论如何,过去两天我一直在摸不着头脑,想弄清楚为什么我一直得到这个错误&#34;反向&#39; post_share&#39;有参数&#39;(&#39;&#39;,)&#39;未找到。尝试了2种模式:[&#39;博客\ /(?P [0-9] +)\ /分享\ / $&#39;,&#39;博客\ / post_share \ / $&#39 ]&#34;

我正在使用Django 2,而我正在读安东尼奥·梅尔(Antonio Mele)的“代码为Django的示例”一书。

Forms.py

class EmailPostForm(forms.Form):
name = forms.CharField(max_length = 30)
email = forms.EmailField()
to = forms.EmailField()
comments = forms.CharField(required = False, widget = forms.Textarea)

Views.py

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
                                            })

urls.py

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`

1 个答案:

答案 0 :(得分:0)

尝试在函数开头声明runnableto变量,就像这样..

name
相关问题