python - 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 →</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 %}
当我单击下载按钮时,我收到上述错误。这里有什么错误?如何纠正它?我是新手。请帮我。提前致谢!
解决方案
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
推荐阅读
- node.js - 如何在猫鼬子文档的子文档中添加文档
- php - Docker php 8 nginx 配置
- typo3 - Typo3 TCA 覆盖:将字段添加到插件面板
- javascript - NuxtJS 和 Vue2 我可以为 Recaptcha 编写服务器端代码吗?
- ethereum - Solidity 中 require(send()) 的安全问题
- c++ - 在构造时初始化一个标准未来向量
- r - 代替 ”
我有像“<x”这样的字符串,必须更改为“x”。
例如:
“<0.1”将是“0.1”
我试过这个:
data2[1:nrow(data2),] <- as.data.frame(apply(data2[1:nrow(data2),], 2, function(x) sub("<a*", "", x)))
<- python - 更新 GraphFrame 中的顶点值
- python - 使用 pandas 将 2 个数据框保存到 excel 中的工作表 1 和 2,但要保持 sheet2 与 excel 本身一样?
- r - R garch 库和 Duan (1995) 模型