Dilshad
Dilshad

Reputation: 184

django.core.urlresolvers.NoReverseMatch

I have an apps called FAQ I am using django-vanilla-views, this apps was working but I got an error like in title above but the body description of the error say: NoReverseMatch: Reverse for 'delete_question' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['faq/delete/(?P\d+)/$']

enter image description here

Here is the codes:

from django.db import models
# Create your models here.
from autoslug import AutoSlugField
from taggit.managers import TaggableManager

class QuestionType(models.Model):
    title   = models.CharField(max_length=255, unique=True)
    slug    = AutoSlugField(populate_from='title', unique=True, max_length=255, blank=False)

    def __unicode__(self):
        return u"%s " % (self.title)


class Question(models.Model):
    title       = models.CharField(max_length=255)
    description = models.TextField()
    slug        = AutoSlugField(populate_from='title', max_length=255, unique=True, blank=False)
    category    = models.OneToOneField('QuestionType', blank=False, related_name='question')
    tags        = TaggableManager()

    def __unicode__(self):
        return "%s. %s " % (self.title, self.category)


from .models import Question from vanilla import CreateView, DeleteView, UpdateView, ListView from django.core.urlresolvers import reverse_lazy

# Create your views here.

class QuestionList(ListView):
    model = Question
    template_name = "faq/question_list.jade"


class CreateQuestion(CreateView):
    model = Question
    template_name = "faq/question.jade"
    success_url = reverse_lazy('question_list')


class EditQuestion(UpdateView):
    model = Question
    template_name = 'faq/question_list.jade'
    success_url = reverse_lazy('question_list')


class DeleteQuestion(DeleteView):
    model = Question
    success_url = reverse_lazy('delete_question')


from django.conf.urls import patterns, url

from .views import QuestionList, CreateQuestion, EditQuestion, DeleteQuestion

urlpatterns = patterns(
    '',
   # URL pattern for the
   url(regex=r'^faq/$', view=QuestionList.as_view(), name='question_list'),
   url(regex=r'^faq/create/$', view=CreateQuestion.as_view(), name='create_question'),
   url(regex=r'^faq/edit/(?P<pk>\d+)/$', view=EditQuestion.as_view(), name='edit_question'),
   url(regex=r'^faq/delete/(?P<pk>\d+)/$', view=DeleteQuestion.as_view(), name='delete_question'),
)

from vanilla import TemplateView

class HomePageView(TemplateView):
    template_name = "pages/home.jade"


class TermsPageView(TemplateView):
    template_name = "templates/question_list.jade"
    pass

configuration/urls.py

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
                       url(r'', include('apps.pages.urls')),
                       url(r'', include('apps.faq.urls')),
                       url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

base.jade

.collapse.navbar-collapse
    ul.nav.navbar-nav.pull-left
        {% url 'home' as home %}
        {% if request.path == home %}
        li.active
            a(href="{% url 'home' %}") {% blocktrans %}Home{% endblocktrans %}
        {% else %}
        li
            a(href="{% url 'home' %}") {% blocktrans %}Home{% endblocktrans %}
        {% endif %}

        {% url 'about' as about %}
        {% if request.path == about %}
        li.active
            a(href="{% url 'about' %}") {% blocktrans %}About us{% endblocktrans %}
        {% else %}
        li
            a(href="{% url 'about' %}") {% blocktrans %}About us{% endblocktrans %}
        {% endif %}

        {% url 'contact' as contact %}
        {% if request.path == contact %}
        li.active
            a(href="{% url 'contact' %}") {% blocktrans %}contact{% endblocktrans %}
        {% else %}
        li
            a(href="{% url 'contact' %}") {% blocktrans %}contact{% endblocktrans %}
        {% endif %}

        {% url 'faq' as faq %}
        {% if request.path == faq %}
        li.active
            a(href="{% url 'question_list' %}") {% blocktrans %}FAQ{% endblocktrans %}
        {% else %}
        li
            a(href="{% url 'question_list' %}") {% blocktrans %}FAQ{% endblocktrans %}
        {% endif %}


        {% if request.user.is_staff %}
        li
            a(href="{% url 'admin:index' %}") {% blocktrans %}Admin{% endblocktrans %}
        {% endif %}

The error is about regex which is not clear why was working and now an error display!! Need some help Thanks

Upvotes: 2

Views: 3691

Answers (3)

isobolev
isobolev

Reputation: 1543

You need to specify 'pk' parameter for 'delete_question' url. Something like this should work:

class DeleteQuestion(DeleteView):
    model = Question

    def get_success_url(self):
        return reverse('delete_question', kwargs={'pk': self.object.pk})

But yes, why do you want DeleteView to redirect to itself after deletion?

Upvotes: 6

Rohan
Rohan

Reputation: 53326

Your success url is

success_url = reverse_lazy('delete_question')

But your delete_question url requires pk as parameter, there no delete_question url without parameter, hence you are getting the error.

Logically your success url should be question_list rather than delete_question

Upvotes: 2

hellsgate
hellsgate

Reputation: 6005

Its because you have 2 url's named 'question_list' ('^faq/$' and ''^faq/delete/(?P\d+)/$''). Change the second one to 'delete_question' and you should be fine

Upvotes: 2

Related Questions