2016-11-21 5 views
1

article.urls.py :Django/Python 예외 값 : 사전 업데이트 시퀀스 요소 # 0의 길이는 4입니다. (2)이 필요

from django.conf.urls import url

from . import views

urlpatterns = [

url(r'^tag/(?P<tag>\w+)/$',views.search_tag,name='search_tag'), 

]

article.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}) 

tag.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 %} 

이 같은 오류가 발생했습니다 이유는 무엇입니까? url에서

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 

, 매개 변수로 '파이썬'는 search_tag(request,tag)에 보내야합니다.

답변

3

요청 컨텍스트 사전을 잘못된 형식으로 보냈습니다.

대신

return render(request,'tag.html',{'post_list',post_list}) 

의 그것은

return render(request,'tag.html', {'post_list': post_list}) 
+0

감사해야합니다! 나는 바보입니다 – weixiang