include()得到了一个意想不到的关键字参数' app_name'

时间:2018-01-09 23:33:00

标签: django django-models django-views django-urls django-2.0

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

File "C:\Users\User\Desktop\djite\djite\djite\urls.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

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

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'),
    ]

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还有一个问题,即当我删除时,我认为这与我当前的错误无关

namespace='blog', app_name='blog'

来自主urls.py

中的这一行
url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')),

服务器运行但是当我转到此目录时:

http://localhost:8000/blog/

我看到了这个错误

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

它说这行代码在views.py

中有问题
 posts=Post.published.all() 

2 个答案:

答案 0 :(得分:10)

在Django 1.9中不推荐使用var t = Date.now(); $('head').append('<link rel="stylesheet" href="style2.css?t=' + t + '" type="text/css" />'); app_name,但在Django 2.0中不起作用。请改为在include中设置app_name

blog/urls.py

然后将include更改为:

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

您不需要url(r'^blog/', include('blog.urls')), ,因为它无论如何都会默认使用应用程序命名空间。

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

namespace='blog'

答案 1 :(得分:2)

在Django 2.0 app_name中,不支持:

使用项目网址:

from django.contrib import admin
from django.urls import include, path

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