Django NoReverseMatch url - django

I can't figure out why I'm returning the following error:
NoReverseMatch at /
Reverse for '' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Here is the link in my template:
<li>Home</li>
Here are my main urls:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^', include('merged.catalog.urls')),
(r'^cart/', include('merged.cart.urls')),
(r'^checkout/', include('merged.checkout.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Here is the sub urls:
from django.conf.urls import patterns, url, include
urlpatterns = patterns('merged.catalog.views',
(r'^$','index', {'template_name': 'catalog/index.html'}, 'catalog_home'),
)
It seems like everything is in order, but maybe I'm missing something obvious.

Some changes that might help.
In your template:
<li>Home</li>
In your urls.py
from django.conf.urls import patterns, url, include
urlpatterns = patterns('merged.catalog.views',
(r'^$','index', {'template_name': 'catalog/index.html'}, name='catalog_home'),
)

Related

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(/?)$']
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
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.
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

Django URL in template NoReverseMatch

I keep getting a NoReverseMatch in my base template, though I've specified a namespace and have put the proper name.
Error:
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['forum/|^forums/$']
Main urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from forum.views import main_home
urlpatterns = [
url(r'^$', main_home, name='home'),
url(r'^admin/', admin.site.urls),
url(r'^accounts/|^account/', include('accounts.urls',
namespace='accounts')),
url(r'^forum/|^forums/', include('forum.urls', namespace='forum')),
]
Forum urls.py:
from django.conf.urls import url
from forum import views
urlpatterns = [
url(r'^$', views.home, name='home'),
]
From my template:
<a class="nav-link" href="{% url 'forum:home' %}">Forum</a>
The docs for reverse() say You cannot reverse url patterns that contain alternative choices using |.
In this case, you could change the URL to:
url(r'^forums?/', include('forum.urls', namespace='forum')),
However, it might be better to choice a single URL /forums/ or /forum/. Having a single URL can be better for SEO.

Including Django REST Framework urls returns 404

I get a 404 showing the following url patterns when I try to browse to http://localhost:8000/api/v1/api-dev/:
^api/v1/ ^api-dev/ ^login/$ [name='login']
^api/v1/ ^api-dev/ ^logout/$ [name='logout']
What's up with the whitespace and ^?
Here's the relevant code from the app.urls:
from django.conf.urls import patterns, url, include
urlpatterns = patterns('',
url(r'^api/v1/', include('api.urls')),
)
And from the api.urls:
from django.conf.urls import patterns, url, include
from rest_framework import routers
from api.views import views
urlpatterns = patterns('',
url(r'^api-dev/', include('rest_framework.urls', namespace='rest_framework')),
)
Oh, the answer is just that I'm an idiot... there is no
http://localhost:8000/api/v1/api-dev/
Only http://localhost:8000/api/v1/api-dev/login
Try removing the url part from both:
(r'^api/v1/', include('api.urls')),
(r'^api-dev/', include('rest_framework.urls', namespace='rest_framework')),

django-registration URL cannot be resolved

I am using django 1.5.1 with django-registration 1.0.
I am getting an error:
NoReverseMatch at /accounts/password/reset/
Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '()' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://localhost:8000/accounts/password/reset/
Django Version: 1.5.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '()' and keyword arguments '{}' not found.
In my urls.py I have:
url(r'^accounts/', include('registration.backends.default.urls', namespace='re gistration', app_name='registration')),
Anyone experience issues with this before?
Just include the following lines and it should work:
url(r'accounts/', include('django.contrib.auth.urls')),
My urls.py:
```
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'django_registration_demo.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'accounts/', include('django.contrib.auth.urls')),
url(r'^accounts/', include('registration.backends.default.urls'))
)
```

NoReverseMatch error in Django [Pinax]

My Django project based on pinax-social fails loading any page which has {% url home %} in it, and shows this:
NoReverseMatch at /account/login/
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found.
Hardcoding the url fixes the problem, and only the home ReverseMatch fails.
Here's my urls.py:
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from .views import *
from NEOreka.models import *
from .forms import SignupForm
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns("neo.views",
url(r"^neo/(?P<neo_id>\d+)/$", "neo_info"),
)
urlpatterns += patterns("",
url(r"^$", "neo.views.home"),
)
urlpatterns += patterns("",
url(r"^admin/", include(admin.site.urls)),
url(r"^account/signup/$", SignupView.as_view(), name="account_signup"),
url(r"^account/", include("account.urls")),
)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Could someone tell me how I could fix this?
Ok I test this one and it works. I hope it will works also in your side.
{% url neo.views.home %}