Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

url() got an unexpected keyword argument 'namespace'

Tags:

python

django

New to Django here, so pardon the rookie question...

I'm getting the url() got an unexpected keyword argument 'namespace' error when I try to call on the reverse function. Tried a few different ways to fix it but nothing seems to be working. Any advice would be greatly appreciated. Please see my code below:

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})

comments/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'),
]
like image 746
Phil H Avatar asked Jul 02 '16 02:07

Phil H


Video Answer


1 Answers

You can refer the syntax and options for the url include() method in official django documentation here. You need to use the namespace inside the include() method and not in the outside url() method.

url(r'^comments/', include('comments.urls', namespace='comments')),
like image 192
Sivasubramaniam Arunachalam Avatar answered Sep 21 '22 12:09

Sivasubramaniam Arunachalam