首页 \ 问答 \ Django / Python异常值:字典更新序列元素#0的长度为4;(Django/Python Exception Value: dictionary update sequence element #0 has length 4; 2 is required)

Django / Python异常值:字典更新序列元素#0的长度为4;(Django/Python Exception Value: dictionary update sequence element #0 has length 4; 2 is required)

article.urls.py:

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

为什么我得到这样的错误?

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)


article.urls.py :

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

Why i got error like this?

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

In the url, 'Python' as the parameter should send to the search_tag(request,tag).


原文:https://stackoverflow.com/questions/40715409
更新时间:2019-12-03 08:15

最满意答案

您正在以不正确的格式发送请求上下文字典。

代替

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

它应该是

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

You are sending the request context dictionary in incorrect format.

Instead of

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

It should be

return render(request,'tag.html', {'post_list': post_list})
2016-11-21

相关问答

更多

自定义更新gridview和ObjectDataSource(Custom update gridview and ObjectDataSource)

您可以在模板字段中放置一个linkbutton并将其CommandName="MyUpdate"为CommandName="MyUpdate" 。 然后,您可以检查gridview的rowCommand事件上的commandName并在rowCommand处理您的自定义更新。 if(e.CommandName =="MyUpdate") { // Perform your update } You can place a linkbutton inside a template f

cocos2d 2.1和HKTMXLayer不透明度问题(cocos2d 2.1 and HKTMXLayer opacity problems)

不确定你的整体上下文,但同一类的绘制方法(第465行)只是设置'position'属性:考虑到类派生自'CCNode',这是正常的。 您正在设置的着色器(通常用于精灵)需要顶点属性以及颜色属性。 有关示例,请参阅CCSprite(draw)中的代码。 也许还有另一种方法可以满足您的需求('为什么'您将Node衍生类的着色器更改为通常用于CCSprite /纹理的着色器)。 The fix was posted here. Along with changing the shader to kCC

inf输出计算线斜率(inf output computing line slopes)

这一行: LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0])); 有一个除零误差。 我相信这个等式应该是: LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointA[0])); This line: LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0])); has a divide by zero error. I believ

OutputCache排除Mvc 4中的布局(OutputCache exclude Layout in Mvc 4)

甜甜圈缓存和甜甜圈洞做类似的事情,只是尝试这个例子http://www.dotnet-tricks.com/Tutorial/mvc/ODJa210113-Donut-Caching-and-Donut-Hole-Caching-with-Asp.Net-MVC -4.html Donut Caching and Donut Hole does similar thing, just try this example http://www.dotnet-tricks.com/Tutorial/m

S3 PUT命令的标题问题(Header Issue with S3 PUT Command)

有一个问题 - 我需要将JS ACL => 'public-read'从JS代码移到签名请求中。 Dropzone发送功能变为: sending: function(file, xhr) { var _send = xhr.send; xhr.send = function() { _send.call(xhr, file); } } PHP签名请求变成: $cmd = $s3Client->getCommand('PutObject', [ 'Bu

为什么功能修改列表在主文件中看不到时会改变?(Why function-modified lists don't change when seen in the main file?)

这一行: a=new_a[:] 用新对象覆盖变量a 。 在函数或模块之外,旧对象仍由(或其在那里调用的任何东西)指向。 尝试: new_a = [v for i, v in enumerate(a) if i not in aleaindex] while a: a.pop() a[:] = new_a[:] 说明 要查看此内容,请尝试以下操作。 >>> a = [1,2,3,4] >>> b = a >>> print b [1, 2, 3, 4] >>> a[:] = [

JS对象:迭代到第n级(JS object: Iteration till nth level)

您需要引用iterateObject内部的本地obj而不是全局jsonData 。 function iterateObject(obj) { for (var key in obj) { if (obj.hasOwnProperty(key)) { if (!isLeafNode(obj[key])) { iterateObject(obj[key]); } else {

Keras:flow_from_directory在fit_generator调用期间保存了太多图像(Keras: flow_from_directory saving too many images during fit_generator call)

fit_generator函数有两个额外的参数,可能是观察到的行为的原因: 默认值为=1意味着将使用额外的线程来加载图像。 线程将从提供的生成器中获取图像并将它们放入队列中。 max_queue_size ,默认值=10是此队列的最大长度。 当对fit_generator的调用返回时, fit_generator线程将停止,但在它继续用图像填充队列之前,即使其中一些不再需要,也会停止。 The fit_generator function has two additional parameter

相关文章

更多

最新问答

更多
  • 根据Woocommerce中的自定义字段计算自定义购物车商品价格(Custom cart item price calculation based on dimentions custom fields in Woocommerce)
  • 分开foreach项目并单独打印(Separate foreach items and print them individually)
  • 写模式管道是否同步?(Is write mode pipe synchronous?)
  • 发现可用的Windows Phone强调色和本地化名称(Discover available Windows Phone accent colors and localized names)
  • 使用多线程和WPF更新集合(Update Collection with multithreading and WPF)
  • 如何在字符串的情况下使用同步?(How should the synchronization be used in case of strings?)
  • 打印唯一的行,比较不超过N个字符(Print unique lines, compare no more than N characters)
  • Javascript / jQuery - 如何调用switch case从另一个函数执行(Javascript/jQuery - how to call a switch case to execute from another function)
  • Hbase超时错误不断发生(Hbase timeout errors keep occuring)
  • 如何在Delphi中更改TabControl中活动TAB的颜色(How to change the color of active TAB in a TabControl, in Delphi)
  • 正则表达式:在sublime文本中替换一些PHP代码(Regex: replace some pieces of php code in sublime text)
  • 在带有Scene2D的LibGDX中,如何在按下按钮时连续向右走?(In LibGDX with Scene2D, how can I continuously walk to the right when a button is pressed?)
  • 累加器如何在Haskell中工作?(How do accumulators work in Haskell?)
  • 使用开关检查市场和前缀与正确的货币符号(using switch to check market and prefix with correct currency symbol)
  • 在哪里分配一次使用类?(Where to allocate one time use class?)
  • 如何从两个DateTime / NaiveDateTime获取持续时间?(How do I get Duration from two DateTime / NaiveDateTime?)
  • 解析TimeSpan大于24小时?(Parse a TimeSpan greater than 24 hours? [duplicate])
  • 如何在球拍中本地更改阅读规则?(How to locally change reading rules in racket?)
  • 数据库应该由DI注入时的模拟存储(通过构造函数)(Mock storage when database should be injected by DI (through constructor))
  • 使用maven集成2个eclipse项目(Integrate 2 eclipse projects using maven)
  • 角度ng-repeat不检测变化(Angular ng-repeat not detecting changes)
  • Xaml组件在Silverlight + XNA应用程序的多个页面中可见(Xaml component visible in multiple pages in Silverlight+XNA applications)
  • 如何将`var`变量等同于另一个查询(How to equate `var` variable to another query)
  • 如何设计hyperledger链代码以适合您的业务?(how to design your chaincode of hyperledger to fit for your business?)
  • PHP readfile错误(PHP readfile error)
  • 在Date之后排序列表然后是时间(Sorting list after Date then time)
  • Android内部版本号(Android build number)
  • 在没有预设退出条件的情况下停止无限循环(stopping an infinite loop with no preset exit condition)
  • Phonegap应用程序全屏通过html页面中的按钮(Phonegap Application fullscreen through a button in html page)
  • PAA是否适合在门户网站中自动执行wcm库部署和设置?(Is PAA a good candidate for automating wcm library deployment and setup in portal?)