Django - 无法将页面标题作为模板中的 url 参数传递
Django - Unable to pass page title as url parameter in template
我有一个大问题。
我需要在模板
中将文章标题传递给 URL
这是我的urls.py
urlpatterns = [
***
re_path(r'^view/(?P<id>\w+)/(?P<title>\w+)/$', views.view, name='view'),
***
]
view.py
def view(request, id, title):
return render(request, 'view.html', {'id' : id, 'title' : title})
一开始我试过
<a href="{% url 'view' id=3685 title='What a terrible weather' %}">What a terrible weather</a>
我收到错误消息:
NoReverseMatch at /view/7/6999/
Reverse for 'view' with keyword arguments '{'id': 3685, 'title': 'What a terrible weather'}' not found. 1 pattern(s) tried: ['view/(?P<id>\w+)/(?P<title>\w+)/$']
***
Error during template rendering
In template D:\Django\gtunews\templates\view.html, error at line 8 Reverse for 'view' with keyword arguments '{'id': 3685, 'title': 'What a terrible weather'}' not found.
1 pattern(s) tried: ['view/(?P<id>\w+)/(?P<title>\w+)/$']
***
然后我试了
{% with thevarable= "What a terrible weather" %}
<a href="{% url 'view' id=3685 title=thevarable %}">What a terrible weather</a>
{% endwith %}
结果:
TemplateSyntaxError at /view/7/6999/
'with' expected at least one variable assignment
***
Error during template rendering
In template D:\Django\gtunews\templates\view.html, error at line 8
'with' expected at least one variable assignment
***
我也试过了
{% with my_var = 'What a terrible weather'.replace(' ','_') %}
<div>Hello, {{my_var}}!</div>
{% endwith %}
结果我犯了同样的错误:
TemplateSyntaxError at /view/7/6999/
'with' expected at least one variable assignment
***
Error during template rendering
In template D:\Django\gtunews\templates\view.html, error at line 8
'with' expected at least one variable assignment
***
然后我试了
{{ title|urlencode:"What a terrible weather" }}
结果:令我惊讶的是它返回了 ID 而不是 Title
我也试过了。这是唯一可行的方式。它只有在我们传递一个单词作为参数时才有效 BUT 谁需要它?是个废话表格
<a href="{% url 'view' id=3685 title='idontneedtopassanparameterlikethat' %}">idontneedtopassanparameterlikethat</a>
正如我在时钟上看到的那样,我花了四个小时来搜索我找不到的解决方案最后,我认为这可能是我在 Django 框架上的最后一个项目。
时间比金钱更昂贵。我什至在Django Framework上花了很多时间,甚至感到厌烦。
请帮我找到答案。
尝试在路径上使用字符串
path('view/<int:id>/<str:title>/', views.view, name='view'),
我有一个大问题。 我需要在模板
中将文章标题传递给 URL这是我的urls.py
urlpatterns = [
***
re_path(r'^view/(?P<id>\w+)/(?P<title>\w+)/$', views.view, name='view'),
***
]
view.py
def view(request, id, title):
return render(request, 'view.html', {'id' : id, 'title' : title})
一开始我试过
<a href="{% url 'view' id=3685 title='What a terrible weather' %}">What a terrible weather</a>
我收到错误消息:
NoReverseMatch at /view/7/6999/
Reverse for 'view' with keyword arguments '{'id': 3685, 'title': 'What a terrible weather'}' not found. 1 pattern(s) tried: ['view/(?P<id>\w+)/(?P<title>\w+)/$']
***
Error during template rendering
In template D:\Django\gtunews\templates\view.html, error at line 8 Reverse for 'view' with keyword arguments '{'id': 3685, 'title': 'What a terrible weather'}' not found.
1 pattern(s) tried: ['view/(?P<id>\w+)/(?P<title>\w+)/$']
***
然后我试了
{% with thevarable= "What a terrible weather" %}
<a href="{% url 'view' id=3685 title=thevarable %}">What a terrible weather</a>
{% endwith %}
结果:
TemplateSyntaxError at /view/7/6999/
'with' expected at least one variable assignment
***
Error during template rendering
In template D:\Django\gtunews\templates\view.html, error at line 8
'with' expected at least one variable assignment
***
我也试过了
{% with my_var = 'What a terrible weather'.replace(' ','_') %}
<div>Hello, {{my_var}}!</div>
{% endwith %}
结果我犯了同样的错误:
TemplateSyntaxError at /view/7/6999/
'with' expected at least one variable assignment
***
Error during template rendering
In template D:\Django\gtunews\templates\view.html, error at line 8
'with' expected at least one variable assignment
***
然后我试了
{{ title|urlencode:"What a terrible weather" }}
结果:令我惊讶的是它返回了 ID 而不是 Title
我也试过了。这是唯一可行的方式。它只有在我们传递一个单词作为参数时才有效 BUT 谁需要它?是个废话表格
<a href="{% url 'view' id=3685 title='idontneedtopassanparameterlikethat' %}">idontneedtopassanparameterlikethat</a>
正如我在时钟上看到的那样,我花了四个小时来搜索我找不到的解决方案最后,我认为这可能是我在 Django 框架上的最后一个项目。 时间比金钱更昂贵。我什至在Django Framework上花了很多时间,甚至感到厌烦。
请帮我找到答案。
尝试在路径上使用字符串
path('view/<int:id>/<str:title>/', views.view, name='view'),