这里是Django的新手,所以请原谅新秀的问题......
当我尝试呼叫url() got an unexpected keyword argument 'namespace'
功能时,我收到reverse
错误。尝试了几种不同的方法来修复它,但似乎没有任何工作。任何建议将不胜感激。请参阅下面的代码:
TypeError at /comments/topic-create/
url() got an unexpected keyword argument 'namespace'
Request Method: POST
Request URL: http://127.0.0.1:8000/comments/topic-create/
Django Version: 1.8.6
Exception Type: TypeError
Exception Value:
url() got an unexpected keyword argument 'namespace'
models.py
from django.db import models
from django.conf import settings
from django.utils.text import slugify
from django.core.urlresolvers import reverse
class Topic(models.Model):
title = models.CharField(max_length=50)
stock = models.ManyToManyField(Stock)
slug = models.SlugField(max_length=200, blank=True)
created = models.DateField(auto_now_add=True,
db_index=True)
def __str__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Topic, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('comments:topic_detail', args=[self.id, self.slug])
forms.py
from django import forms
from .models import Topic
class TopicCreateForm(forms.ModelForm):
class Meta:
model = Topic
fields = ('__all__')
views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .forms import TopicCreateForm
from django.contrib import messages
from .models import Stock, Topic, Comment
# from django.core.urlresolvers import reverse_lazy
@login_required
def topic_create(request):
if request.method == 'POST':
form = TopicCreateForm(data=request.POST)
if form.is_valid():
cd = form.cleaned_data
new_item = form.save(commit=False)
new_item.user = request.user
new_item.save()
messages.success(request, 'Topic added successfully')
# redirect to new created topic detail view
return redirect(new_item.get_absolute_url())
else:
form = TopicCreateForm(data=request.GET)
return render(request,
'comments/comment/topic-create.html',
{'section': 'topic',
'form': form})
def topic_detail(request, namespace, id, slug):
topic = get_object_or_404(Topic, id=id, slug=slug)
return render(request,
'comments/comment/topic-detail.html',
{'section': 'topic',
'topic': topic})
评论/ urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^topic-create/$', views.topic_create, name='create-topic'),
url(r'topic-detail/(?P<id>\d+)/(?P<slug>[-\w]+)/$',
views.topic_detail, name='topic-detail'),
]
mysite的/ urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^account/', include('account.urls')),
url(r'^comments/', include('comments.urls'),
namespace='comments'),
]
答案 0 :(得分:7)
您可以在官方django文档here中引用网址include()
方法的语法和选项。您需要在include()
方法中使用命名空间,而不是在外部url()
方法中使用。
url(r'^comments/', include('comments.urls', namespace='comments')),
答案 1 :(得分:2)
include()
期望关键字参数namespace
不是url()
,因此mysite/urls.py
中的最后一行应为
url(r'^comments/', include('comments.urls', namespace='comments')),