字典更新序列元素#0的长度为1; Django需要2

时间:2018-01-01 21:15:05

标签: python django

我正在使用Python 3.5.4和Django 2.0.0 final 0.我正在尝试创建一个Django博客Web应用程序。我创建了Django视图和模板后收到错误。这是我的错误:

Internal Server Error: /blog
Traceback (most recent call last):
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/utils/deprecation.py", line 93, in __call__
    response = self.process_request(request)
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/middleware/common.py", line 60, in process_request
    if self.should_redirect_with_slash(request):
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/middleware/common.py", line 79, in should_redirect_with_slash
    is_valid_path('%s/' % request.path_info, urlconf)
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/urls/base.py", line 155, in is_valid_path
    resolve(path, urlconf)
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/urls/base.py", line 24, in resolve
    return get_resolver(urlconf).resolve(path)
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 496, in resolve
    sub_match = pattern.resolve(new_path)
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 496, in resolve
    sub_match = pattern.resolve(new_path)
  File "/home/pecan/PycharmProjects/autisticstory/venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 345, in resolve
    kwargs.update(self.default_args)
ValueError: dictionary update sequence element #0 has length 1; 2 is required
[01/Jan/2018 21:12:43] "GET /blog HTTP/1.1" 500 88570

此处 autisticstory / urls.py 文件:

"""autisticstory URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls',
                          namespace='blog')),
]

blog / views.py 文件:

from django.shortcuts import render
from .models import Post


# Create your views here.
def post_list(request):
    posts = Post.published.all()

    return render(request, 'blog/post/list.html',
                  {'posts': posts})

blog / urls.py 文件:

from django.conf.urls import url
from . import views

app_name = 'blog'

urlpatterns = [
    url('^$', views.post_list, 'post_list'),
]

blog / models.py 文件:

from django.contrib.auth.models import User
from django.db import models
from django.utils import timezone


# Create your models here.
class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Roboczy'),
        ('published', 'Opublikowany')
                      )

    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,
                            unique_for_date='publish')
    author = models.ForeignKey(User,
                               related_name='blog_posts',
                               on_delete=models.CASCADE)
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,
                              choices=STATUS_CHOICES,
                              default='draft')

    class Meta:
        ordering = ('-publish',)

    class PublishedManager(models.Manager):
        def get_queryset(self):
            return super().get_queryset()\
                .filter(status='published')

    published = PublishedManager()

    def __str__(self):
        return self.title

templates / blog / list.html 文件:

{% extends "blog/base.html" %}

{% block content %}
    {% for post in posts %}
        <article class="blog-post">
            <div class="blog-post-title">
                <h2>{{ post.title }}</h2>
            </div>
            <div class="blog-post-meta">
                <p>Published {{ post.publish }} by {{ post.author }}</p>
            </div>
            <hr/>
            <div class="post-content">
                {{ post.body|truncatewords:300 }}
            </div>
            <div class="post-footer">
                Tags: <a href="#">hello</a> | <a href="#">world</a>
            </div>
        </article><!-- /.blog-post -->
    {% empty %}
        <p>Nie znaleziono żadnych postów!</p>
    {% endfor %}
{% endblock %}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

它与blogs/urls.py中的这一行有关:

urlpatterns = [
    url('^$', views.post_list, 'post_list'),
]

这需要像:

urlpatterns = [
   url('^$', views.post_list, name='post_list'),
]

第三个参数是kwargs,而不是name,因此您需要指定此参数。有关详细信息,请参阅here