我刚开始使用Django模块进行编码,现在我只是从Lynda复制代码。 虽然我编码正确,但在尝试运行服务器时遇到了大量错误。
我想这是因为Django 2和Django 1.8之间存在差异。
我到底应该改变什么?您可能会在下面找到我的代码和错误。谢谢。
错误:
python manage.py runserver
执行系统检查......
.wrapper在0x105a53598开始的线程中的未处理异常>追溯(最近的 最后打电话):文件 " /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/urls/conf.py" ;, 第17行,包括在内 urlconf_module,app_name = arg ValueError:要解包的值太多(预期为2)
在处理上述异常期间,发生了另一个异常:
Traceback(最近一次调用最后一次):文件 " /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/utils/autoreload.py" ;, 第225行,在包装中 fn(* args,** kwargs)File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/core/management/commands/runserver.py", 第121行,在inner_run中 self.check(display_num_errors = True)File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/core/management/base.py", 364行,正在检查中 include_deployment_checks = include_deployment_checks,File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/core/management/base.py", 第351行,在_run_checks中 return checks.run_checks(** kwargs)File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/core/checks/registry.py", 第73行,在run_checks中 new_errors = check(app_configs = app_configs)File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/core/checks/urls.py", 第40行,在check_url_namespaces_unique中 all_namespaces = _load_all_namespaces(resolver)File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/core/checks/urls.py", 第57行,在_load_all_namespaces中 url_patterns = getattr(resolver,' url_patterns',[])File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/utils/functional.py" , 第36行,获取 res = instance。 dict [self.name] = self.func(instance)File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/urls/ resolvers.py&#34 ;, 第536行,在url_patterns中 patterns = getattr(self.urlconf_module," urlpatterns",self.urlconf_module)文件 " /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/utils/functional.py" ;, 第36行,获取 res = instance。 dict [self.name] = self.func(instance)File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/urls/ resolvers.py&#34 ;, 第529行,在urlconf_module中 return import_module(self.urlconf_name)File" /Users/keremkizbaz/anaconda3/lib/python3.6/importlib/ init .py", 第126行,在import_module中 在_gcd_import文件中返回_bootstrap._gcd_import(name [level:],package,level)文件"",第994行 "",第971行,在_find_and_load文件中 "",第955行,在_find_and_load_unlocked中 文件"",第665行,在_load_unlocked中 文件"",第678行,in exec_module文件"",第219行,in _call_with_frames_removed File" /Users/keremkizbaz/mysite/mysite/urls.py" ;,第22行,在 url(r' ^ admin /',include(admin.site.urls)),File" /Users/keremkizbaz/anaconda3/lib/python3.6/site-packages/django/urls/ conf.py&#34 ;, 第27行,包括在内 '为include()提供名称空间参数。' %len(arg)django.core.exceptions.ImproperlyConfigured:传递3元组到 不支持include()。传递包含列表的2元组 patterns和app_name,并为include()提供namespace参数 代替。
models.py:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get.queryset().filter(status = 'published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length= 250)
slug = models.SlugField(max_length = 250, unique_for_date = 'publish')
author = models.ForeignKey(User, on_delete = models.CASCADE , related_name = 'blog_posts')
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')
objects = models.Manager() # the default manager
published = PublishedManager() # the Dahl-specific manager.
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', args = [self.publish__year,
self.publish.strftime('%m'),
self.publosh.strftime('%d'),
self.slug])
views.py:
from django.shortcuts import render, get_object_or_404
from .models import Post
def post_list(request):
posts = Post.published.all()
return render(request, 'blog/post/list.html',{'posts': posts})
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug = post,
status = 'published',
publish__year = year,
publish__month = month,
publish__day = day)
return render(request, 'blog/post/list.html',{'posts': posts})
URLS.PY
from django.shortcuts import url from . import views from django.contrib import admin
urlpatters = [
url(r'^$', views.post_list, name = 'post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<post>[-\w]+)/$',
views.post_detail,
name = 'post_detail'), ]
URL.py 2:
from django.contrib import admin
from django.urls import path
from django.conf.urls import include,url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls',namespace = 'blog', app_name = 'blog')),
]