django-registration URL cannot be resolved - django

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'))
)
```

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.

Django NoReverseMatch url

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'),
)

Django urlresolvers: Need more than 2 values to unpack

I'm trying to upgrade from Django 1.3 to Django 1.4. I'm stuck with this error:
Python Version: 2.7.3
Django Version: 1.4.10
Exception Type: ValueError
Exception Value: need more than 2 values to unpack
The line that triggers that error is (In template /var/www/proj/src_1.4/templates/fragments/header.html, error at line 20):
<a id="login" href="{% url login %}" rel="nofollow">{% trans "Login" %}</a>
It works fine in Django 1.3.
I've tried the following:
python manage.py shell
>> from django.conf.urls import *
>> from django.core.urlresolvers import reverse
>> reverse('login')
Then this error appears:
ValueError Traceback (most recent call last)
/var/www/proj/env_1.4/local/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 reverse('login')
/var/www/proj/env_1.4/local/lib/python2.7/site-packages/django/core/urlresolvers.pyc in reverse(viewname, urlconf, args, kwargs, prefix, current_app)
474 resolver = get_ns_resolver(ns_pattern, resolver)
475
--> 476 return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
477
478 reverse_lazy = lazy(reverse, str)
/var/www/proj/env_1.4/local/lib/python2.7/site-packages/django/core/urlresolvers.pyc in _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs)
363 possibilities = self.reverse_dict.getlist(lookup_view)
364 prefix_norm, prefix_args = normalize(_prefix)[0]
--> 365 for possibility, pattern, defaults in possibilities:
366 for result, params in possibility:
367 if args:
ValueError: need more than 2 values to unpack
If I look at the info showed by Django when I try to load my project, "Local vars" shows that info:
self <RegexURLResolver urls (None:None) ^/>
args ()
_prefix u'/'
possibilities [([(u'accounts/login/', [])], 'accounts/login/$')]
lookup_view u'login'
prefix_norm u'/'
prefix_args []
kwargs {}
Code in proj/urls.py
from django.conf.urls import *
urlpatterns += patterns('',
url(r'^', include('home.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts', include('accounts.urls')),
Code in apps/accounts/urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^/register$',
'accounts.views.register',
name='register'),
url(r'^/login/$',
'django.contrib.auth.views.login',
{'template_name': 'accounts/login.html', 'authentication_form': AuthenticationForm},
name='login'),
I'll appreciate any help about that. Thanks.
Finally I found the error. It was an old app used for internationalization of URLs (i18nurls). Django 1.3 uses an external app, in Django > 1.4 internationalization was included in the core (Django: Internationalization: in URL patterns).
Thanks.
Not sure if this will fix your errors, but try to do the following:
In your main url.py replace
url(r'^accounts', include('accounts.urls')),
with
url(r'^accounts/', include('accounts.urls')), ### add the slash /
It is better to include the slash / in the outer urlpatterns rather then in the inner one.
Then, in the urls.py inside accounts replace
url(r'^/register$',...
url(r'^/login/$',....
with
url(r'^register/$',... ### delete dhe leading slashed, because you added it in `accounts/`
url(r'^login/$',.... ### finish both regex with the slash
Another suggestion would be to add a namespace:
url(r'^accounts/', include('accounts.urls', namespace='accounts')),
Now refer inside to your template with "{% url 'accounts:login' %}"
In this way you will know better which url comes from which app. Check if these tips fixed your bug and if no, let me know
If this is your full urls.py code
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^/register$',
'accounts.views.register',
name='register'),
url(r'^/login/$',
'django.contrib.auth.views.login',
{'template_name': 'accounts/login.html', 'authentication_form': AuthenticationForm},
name='login'),
check you have not added the closing bracket )
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^/register$',
'accounts.views.register',
name='register'),
url(r'^/login/$',
'django.contrib.auth.views.login',
{'template_name': 'accounts/login.html', 'authentication_form': AuthenticationForm},
name='login'),
)

Django: 404 error in urls.py

I have two urls.py files.
In project/urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)
In project/blog/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('blog.views',
(r'^$', 'index'),
(r'^(?P<slug>[a-z-]+)/$', 'detail'),
(r'^(?P<slug>[a-z-]+)/comment/$', 'comment'),
)
Then I tried to browse these URLs:
http://127.0.0.1:8888/ (404)
http://127.0.0.1:8888/hello-world/ (404)
http://127.0.0.1:8888/admin/ (It worked)
Django version: 1.4 pre-alpha SVN-16985.
Thanks!
To fix the http://127.0.0.1:8888/ error, change your regular expression to:
(r'^$', include('blog.urls')),
Regarding the http://127.0.0.1:8888/hello-world/ error: There could be many things causing this. First, check that you have a blog title that actually returns the slug 'hello-world'.