作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
文章.urls.py:
从 django.conf.urls 导入 url
来自 .导入 View
urlpatterns = [
url(r'^tag/(?P<tag>\w+)/$',views.search_tag,name='search_tag'),
]
文章.views.py:
from django.shortcuts import render
from django.http import HttpResponse
from article.models import Article
def search_tag(request,tag):
try:
post_list = Article.objects.all()
except Article.DoesNotExist:
raise Http404
return render(request,'tag.html',{'post_list',post_list})
标签.html:
{% extends "base.html" %}
{% load custom_markdown %}
{% block content %}
<div class="posts">
{% for post in post_list %}
<section class="post">
<header class="post-header">
<h2 class="post-title"><a href="{% url "detail" id=post.id %}">{{ post.title }}</a></h2>
<p class="post-meta">
Time: <a class="post-author" href="#">{{ post.date_time |date:"Y M d"}}</a> <a class="post-category post-category-js" href="{% url "search_tag" tag=post.category %}">{{ post.category|title }}</a>
</p>
</header>
<div class="post-description">
<p>
{{ post.content|custom_markdown }}
</p>
</div>
<a class="pure-button" href="{% url "detail" id=post.id %}">Read More >>> </a>
</section>
{% endfor %}
</div><!-- /.blog-post -->
{% endblock %}
为什么我会遇到这样的错误?
ValueError at /tag/Python/
dictionary update sequence element #0 has length 4; 2 is required
Request Method: GET
Request URL: http://localhost:9000/tag/Python/
Django Version: 1.10.3
Exception Type: ValueError
Exception Value:
dictionary update sequence element #0 has length 4; 2 is required
Exception Location: /usr/local/lib/python3.5/dist-packages/django/template/context.py in __init__, line 18
Python Executable: /usr/bin/python3
Python Version: 3.5.2
Python Path:
['/home/weixiang/workspace/WebPy/my_blog',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/home/weixiang/.local/lib/python3.5/site-packages',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Mon, 21 Nov 2016 07:50:07 +0000
在url
中,“Python”作为参数应发送到search_tag(request,tag)
。
最佳答案
您发送的请求上下文字典格式不正确。
而不是
return render(request,'tag.html',{'post_list',post_list})
应该是
return render(request,'tag.html', {'post_list': post_list})
关于python - Django/Python 异常值 : dictionary update sequence element #0 has length 4; 2 is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715409/
我是一名优秀的程序员,十分优秀!