首页 > 解决方案 > Django 中 /13/download/ 的 NoReverseMatch

问题描述

我一直在尝试使用 Django 开发一个小型应用程序。一切正常。然后,我添加了一个“下载”按钮来向用户发送邮件。每当我单击该按钮时,我都会收到此错误:

NoReverseMatch at /13/download/
Reverse for 'about' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)/about/$']

我的意见.py:

def download(request,pk):
if not request.user.is_authenticated:
    return render(request, 'set_goals/index.html')
else:
    user = request.user
    immediate = {}
    necessary = {}
    longterm = {}
    notnecessary = {}
    try:
        immediate = get_object_or_404(Immediate, user_id=pk)
        necessary = get_object_or_404(Necessary, user_id=pk)
        longterm = get_object_or_404(Longterm, user_id=pk)
        notnecessary = get_object_or_404(NotNecessary, user_id=pk)
    except:
        print("error somewhere")
    pdf = render_to_pdf('set_goals/all_goals.html',{'notnecessary':notnecessary,"immediate":immediate,"longterm":longterm,"necessary":necessary})
    if pdf:
        print("yes pdf")
        response = HttpResponse(pdf, content_type='application/pdf')
        filename = "Goals.pdf"
        content = "inline; filename='%s'" % (filename)
        message = EmailMessage("Hello","These are your goals","8888888888@gmail.com",['999999999999999@gmail.com'])
        message.attach('mypdf.pdf',pdf,'application/pdf')
        message.send()
        return render(request,'set_goals/all_goals.html',{'notnecessary':notnecessary,"immediate":immediate,"longterm":longterm,"necessary":necessary})
    return render(request,'set_goals/index.html')

urls.py:

from django.conf.urls import url,include
from . import views

app_name = 'set_goals'

urlpatterns = [
    url(r'^$',views.index,name='index'),
    url(r'^(?P<pk>[0-9]+)/about/$',views.about,name='about'),
    url(r'^(?P<pk>[0-9]+)/download/$', views.download, name='download'),
]

模板 :

    {% extends 'base.html' %}
{% block title %}Immediate{% endblock %}
{% block content %}

<div class="container">
    {% if user.is_authenticated %}
       <div class="container">
           <div class="row">
               <div class="col-md-6">
                   {% if immediate.body %}
                   <h2>{{immediate.title}}</h2>
                   <pre>{{immediate.body}}</pre>
                   {% else %}
                   <h2>Immediate Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>

               <div class="col-md-6">
                   {% if necessary.body %}
                   <h2>{{ necessary.title }}</h2>
                   <pre>{{ necessary.body }}</pre>
                   {% else %}
                   <h2>Necessary Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>
           </div>
           <div class="row">
               <div class="col-md-6">
                   {% if longterm.body %}
                   <h2>{{ longterm.title }}</h2>
                   <pre>{{ longterm.body }}</pre>
                   {% else %}
                   <h2>Longterm Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>

               <div class="col-md-6">
                   {% if notnecessary.body %}
                   <h2>{{ notnecessary.title }}</h2>
                   <pre>{{ notnecessary.body }}</pre>
                   {% else %}
                   <h2>Not Necessary Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>
           </div>
           <div class="row">
               <a href="{% url 'set_goals:download' user.id %}" class="btn btn-primary">Download &rarr;</a>
           </div>
       </div>
    {% else %}
        <h2>Please Login First</h2>
    {% endif%}
</div>

{% endblock %}

下载.html:

    {% extends 'base.html' %}
{% block title %}Immediate{% endblock %}
{% block content %}

<div class="container">
    {% if user.is_authenticated %}
       <div class="container">
           <div class="row">
               <div class="col-md-6">
                   {% if immediate.body %}
                   <h2>{{immediate.title}}</h2>
                   <pre>{{immediate.body}}</pre>
                   {% else %}
                   <h2>Immediate Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>

               <div class="col-md-6">
                   {% if necessary.body %}
                   <h2>{{ necessary.title }}</h2>
                   <pre>{{ necessary.body }}</pre>
                   {% else %}
                   <h2>Necessary Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>
           </div>
           <div class="row">
               <div class="col-md-6">
                   {% if longterm.body %}
                   <h2>{{ longterm.title }}</h2>
                   <pre>{{ longterm.body }}</pre>
                   {% else %}
                   <h2>Longterm Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>

               <div class="col-md-6">
                   {% if notnecessary.body %}
                   <h2>{{ notnecessary.title }}</h2>
                   <pre>{{ notnecessary.body }}</pre>
                   {% else %}
                   <h2>Not Necessary Goals</h2>
                   <p>Yet to be placed!</p>
                   {% endif %}
               </div>
           </div>
       </div>
    {% else %}
        <h2>Please Login First</h2>
    {% endif%}
</div>

{% endblock %}

当我单击下载按钮时,我收到上述错误。这里有什么错误?如何纠正它?我是新手。请帮我。提前致谢!

标签: pythondjangodjango-templatesdjango-views

解决方案


 Reverse for 'something' with arguments '('',)' not found. 1 pattern(s) tried: ['some path']

通常在渲染模板中的模板标签有错误时会出现此错误。

django 将在渲染之前检查所有模板标签,如果模板标签中有任何不匹配或不正确,它将引发此异常。

 In download.html there is no about tag may it will be in base.html.check may be it is in correct or update question with base.thml

推荐阅读