【发布时间】:2018-08-02 03:12:03
【问题描述】:
我一直在尝试使用 Django 开发一个小型应用程序。一切正常。然后,我添加了一个“下载”按钮来向用户发送邮件。每当我单击该按钮时,我都会收到此错误:
NoReverseMatch at /13/download/
Reverse for 'about' with arguments '('',)' not found. 1 pattern(s) tried: ['(?P<pk>[0-9]+)/about/$']
我的views.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 →</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 %}
当我点击下载按钮时,我收到了上述错误。这里有什么错误?如何纠正它?我是新手。请帮我。提前致谢!
【问题讨论】:
-
好的,问题可能与
{% url 'about' ...标记有关,在您的download.html中看不到该标记。它可能在您的base.html中。寻找那个模板标签。
标签: python django django-templates django-views