include() 得到了一个意外的关键字参数“app_name" [英] include() got an unexpected keyword argument 'app_name'

查看:28
本文介绍了include() 得到了一个意外的关键字参数“app_name"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 django-2.0 为我的网站制作一个博客应用程序当我运行服务器时,我看到以下错误

i am making a blog application for my website with django-2.0 when i run server i see the following error

File "C:UsersUserDesktopdjitedjitedjiteurls.py", line 7, in <module>
url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')),
TypeError: include() got an unexpected keyword argument 'app_name'

这是我的主要 urls.py

here is my main urls.py

from django.contrib import admin
from django.conf.urls import url,include

urlpatterns = [
    url(r'^admin/', admin.site.urls),

    url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')),
]

这是我的博客/urls.py

and here's my blog/urls.py

from django.conf.urls import url
from . import views
urlpatterns = [
    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'),
    ]

我的views.py:

my views.py:

from django.shortcuts import render, HttpResponse, get_object_or_404
from blog.models import Post
def post_list(request): #list
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, slog=post,
                              status='published',
                              publush__year=year,
                              publish__month=month,
                              publish__day=day)
    return render (request, 'blog/post/detail.html', {'post':post})

models.py:

# -*- coding:utf-8 -*-
from django.db import models
from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
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=255,verbose_name=_('title'),help_text=_('add 
    title'))
    content = models.TextField(verbose_name=_('content'),help_text=_('write 
    here'))
    publish = models.DateTimeField(default=timezone.now)
    createtime = models.DateTimeField(_('create time'),auto_now_add=True, 
    auto_now=False,help_text=_('create time'))
    updatetime = models.DateTimeField(_('update time'),auto_now_add=False, 
    auto_now=True,help_text=_('update time'))
    author = models.ForeignKey(settings.AUTH_USER_MODEL, 
    verbose_name=_('author'), 
    on_delete=models.DO_NOTHING,help_text=_('choose author'))
    slug = models.SlugField(unique=True, max_length=255,help_text=_('add 
    slug'))
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, 
    default='draft')


    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.title

    class Meta:
        ordering = ('-publish',)
        verbose_name = _('Post')
        verbose_name_plural = _('Posts')


    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.publish.year,
                                         self.publish.strftime('%m'),
                                         self.publish.strftime('%d'),
                                         self.slug])

当我删除时,我的 views.py 也有一个问题,我认为这与我当前的错误无关

also my views.py has a problem that i don't think that's related to my current error, when i delete

namespace='blog', app_name='blog'

从主 urls.py 中的这一行

from this line in main urls.py

url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')),

服务器运行,但是当我进入这个目录时:

the server runs but when i go to this directory:

http://localhost:8000/blog/

我看到这个错误

AttributeError at /blog/
type object 'Post' has no attribute 'published'

说这行代码在views.py中有问题

it says that this line of code has problem in views.py

 posts=Post.published.all() 

推荐答案

使用 app_nameinclude 在 Django 1.9 中已被弃用,在 Django 2.0 中不起作用.改为在 blog/urls.py 中设置 app_name.

Using app_name with include is deprecated in Django 1.9 and does not work in Django 2.0. Set app_name in blog/urls.py instead.

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

然后将包含更改为:

url(r'^blog/', include('blog.urls')),

您不需要 namespace='blog',因为它无论如何都会默认为应用程序命名空间.

You don't need namespace='blog', as it will default to the application namespace anyway.

第二个错误无关.您忘记在模型上实例化您的自定义管理器.

The second error is unrelated. You have forgotten to instantiate your custom manager on the model.

class Post(models.Model):
    ...
    published = PublishedManager()

这篇关于include() 得到了一个意外的关键字参数“app_name"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆