gpt4 book ai didi

python - url() 得到了一个意外的关键字参数 'namespace'

转载 作者:行者123 更新时间:2023-11-28 20:04:02 25 4
gpt4 key购买 nike

这里是 Django 的新手,所以请原谅菜鸟问题...

当我尝试调用 reverse 函数时,出现 url() got an unexpected keyword argument 'namespace' 错误。尝试了几种不同的方法来修复它,但似乎没有任何效果。任何建议将不胜感激。请在下面查看我的代码:

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'

模型.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])

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

最佳答案

可以引用django官方文档中url include()方法的语法和选项here .您需要在 include() 方法内部使用命名空间,而不是在外部 url() 方法中。

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

关于python - url() 得到了一个意外的关键字参数 'namespace',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38156069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com