Maverick
Maverick

Reputation: 2760

NoReverseMatch on Django even when kwargs are provided

The Django cant resovle the url, even though the expected kwarg is provided.

Here is root urls.py:

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

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
    url(r'^ckeditor/', include('ckeditor_uploader.urls')),
    url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.STATIC_ROOT}),
    url(r'^(?P<domain>\w+)', include('frontend.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Here is frontend urls.py: from django.conf.urls import include,patterns,url

from . import views
from .views import MyprofileView
from .views import SuccessView
from .views import CompanyView
from .views import SubscriptionView


from django.views.decorators.csrf import csrf_exempt

urlpatterns = patterns('',
    url(r'/success(/?)$', SuccessView.as_view(), name='success'),
    url(r'/subscribe(/?)$', SubscriptionView.as_view(), name='subscribe'),
    url(r'^(/?)$', MyprofileView.as_view(), name='home'),
    url(r'/api/v1/', include('cpprofile.api.urls')),
    url(r'/product', include('product_information.urls')),
    url(r'/corporations/(?P<company>\d+)$', CompanyView.as_view(), name='company_page'),
    url(r'^/(?P<subscription>\w+)/product/pay/return/(?P<amount>\d+)/(?P<currency>\w+)/(?P<id>\d+)?$',
        views.payment_return, name='subscription_product_payment_return'),
)

And here is how I am trying to reverse call it in view.py MyprofileView:

context['subscribe_url'] = redirect('subscribe', kwargs={'domain': 'up'})

What could be wrong here?

Thanks


UPDATE 1

Here is the error I am getting:

django.core.urlresolvers.NoReverseMatch

NoReverseMatch: Reverse for 'subscribe' with arguments '()' and keyword arguments '{'domain': 'up'}' not found. 1 pattern(s) tried: ['(?P<domain>\\w+)/subscribe(/?)$']

Upvotes: 0

Views: 1230

Answers (3)

Bj&#246;rn Kristinsson
Bj&#246;rn Kristinsson

Reputation: 624

You need to use reverse to get the correct URL, and then redirect to that.

from django.core.urlresolvers import reverse

return redirect(reverse('subscribe', kwargs={'domain': 'up'}))

In your case, where you seem to be trying to assign the url to a context variable, you shouldn't use redirect at all. Reverse resolves the URL, redirect returns a response.

context['subscribe_url'] = reverse('subscribe', kwargs={'domain': 'up'})

Might also want to follow best practices with your urlconf for consistency, namely end all patterns with '/', but don't start any with '/'. As you do for most of them in the root config:

url(r'^admin/', include(admin.site.urls)), <-- good

Upvotes: 1

RemcoGerlich
RemcoGerlich

Reputation: 31260

I suspect it's because of the (/?). That captures either '' or '/'. So you have to pass that as a non-keyword argument:

redirect('subscribe', '/', domain='up')

So this is in addition to what Sachin Kukreja says.

Upvotes: 1

Sachin
Sachin

Reputation: 3674

You have to unpack the kwargs.

Solution:

kwargs = {'domain': 'up'}
redirect('app_name:subscribe', **kwargs)

EDIT: This will work, no need to change the url.
EDIT2: Prepend app's name and a colon to url name. This finds the url in the app namespace.

Ref: Redirect in Django

Upvotes: 2

Related Questions