Django: password reset ReverseMatch error - python

I'm having trouble getting password_reset_confirm to work. I have looked at numerous solutions, but none seem to be working for me.
urls.py: (in particular, the third line)
(r'^resetpassword/passwordsent/$', 'django.contrib.auth.views.password_reset_done', name="reset_password"),
(r'^resetpassword/$', 'django.contrib.auth.views.password_reset', name="reset_password"),
(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)-(?P<token>,+)/$', 'django.contrib.auth.views.password_reset_confirm'),
(r'^reset/done/$', 'django.contrib.auth.views.password_reset_complete'),
the password_reset_email.html:
{% load url from future %}
{% autoescape off %}
Someone asked for password reset for email {{ email }}. Follow the link below:
{{ protocol}}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb64=uid token=token %}
{% endautoescape %}
Everything seems to be working fine, until I submit my e-mail and get the following error:
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': 'OQ', u'token': u'3n2-0fee9d3f98dad36e63d8'}' not found. 2 pattern(s) tried: ['/$reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'reset/(?P<uidb64>[0-9A-Za-z_\\-]+)-(?P<token>,+)/$']
I am using Django 1.6.
Any help is much appreciated! Thanks!

You can see from the exception what's going on, although it's a little hard to spot. If you look at what patterns it tried:
2 pattern(s) tried: ['/$reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'reset/(?P<uidb64>[0-9A-Za-z_\\-]+)-(?P<token>,+)/$']
You should notice that the first pattern is the one that should generally match, in the sense that it accepts a token with a - in it. But it has a stray $ prepending the rest of its content, so actually it can't match anything:
'/$reset/...'
You don't show the urls.py line that establishes that pattern - the third line you refer to can only match a token consisting of nothing but commas:
(?P<token>,+)
So while I can safely say that you need to correct your urls.py, I cannot say exactly where you need to correct it. If you intend to match that urls.py line, you should update the token group regexp to accept your actual token values, and should figure out why the other one is around to match at all. That said, if - is a valid character to appear as part of your token I think you'll find it easier overall to use / as a divider between your uidb64 field and your token, as your first regexp does except for the stray $.

Related

Cannot have any URLs with slugs. NoReverseMatch

I'm a begginer grasping at straws with difficulty dealing with the django slug url system and these NoReverseMatch errors that make no sense to me even after reading the docs.
I have a django project. In one of the views, I pass a list of geoJSON features into a template, and show them on a map. I want to have each feature act as a clickable 'link' to a view that will show stuff about it. The following is part of the template that has those features that I want to click on:
//part of the template:
<script type="text/javascript">
...
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
window.location.href = "{% url 'polls:areadetail' feature.properties.myslug%}";
});
}
(I have confirmed that feature.properties.myslug does in fact contain the slug I want).
The url pattern I want to go to:
urlpatterns = [...
url(r'^areadetail/(?P<areaslug>[-\w]+)/$', views.AreaDetail, name='areadetail'),]
And the view it relates to:
def AreaDetail(request, areaslug):
area = get_object_or_404(Area, nameslug=areaslug)
return render(request, 'polls/areadetail.html', {'area':area})
The issue I get is, by doing what I show and placing that url reference inside that template I show above, that I want to be able click on, that template won't even work at all, giving me a 'Error during template rendering' full page error info that starts with:
NoReverseMatch at /polls/areas/
Reverse for 'areadetail' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'polls/areadetail/(?P[-\w]+)/$']
Any help would be immensely appreciated
EDIT part1: As I've said in response to falsetru, I'm sure feature.properties.myslug has in fact got a slug expression in it.
EDIT2: Based on something I found in a django ticket, I've made a slight change in the url regex at urls.py, from (?P<areaslug>[-\w]+)/$ to (?P<areaslug>[-\w]+)?/$ and now the error is:
Page not found (404)
Request Method: GET Request URL: http://127.0.0.1:8000/polls/areadetail// Raised by: polls.views.AreaDetail
Is it possible that because the "{% url 'polls:areadetail' feature.properties.myslug%}" bit is inside javascript, that feature.properties.myslug is not being inserted there correctly? Like some sort of brackets are needed here?
According to the error message, feature.properties.myslug is empty or has no value.
Make sure the feature.properties.myslug is passed correctly from view.
Comment out {% url .. %} temporarily.
Print {{ feature }}, {{ feature.properties }}, {{ feature.properties.myslug }} to see if which part is missing.
Fix view accordingly.
Uncomment {% url .. %}.
After some more digging around I've found the answer to why doesn't this work in another question at:
How to pass javascript variable to django custom filter
The answer to it by Ludwik Trammer says:
Django templates are build on the server side, while JavaScript is executed on the client side.
That means that template code is always executed before JavaScript (as
it is executed by the server, before the page is sent to the client).
As a consequence it is absolutely impossible to mix JavaScript and
Django code the way you want to.
Which clearly applies here. I was focused on problems with the URL template, regex on the urls.py file etc. when the problem was that no matter what I did, because it's in a javascript section, run client-side, that URL template will always be incomplete no matter what I do, therefore being an impossible solution to what I want.

Unexpected keyword argument in django

I am trying to render the url of the movie clicked among numerous other movies. The url should be in the format
url/moviename. However, I am getting this error
movie_detail() got an unexpected keyword argument 'movie_name'
Please help me.... I am very new to Django, as you can see.
views code:
def movie_detail(request, movie_name):
movieneed = movies.objects.get(title = movie_name)
movieneed = movieneed.replace("_", " ")
return render(request, 'app/movie.html', {'movie':movieneed})
template code:
{% for movie in movies %}
<li data-id="{{ movie.id }}"><span class="title">
<a href="{% url "movie" movie_name=movie.movie_name %}">
{{ movie.title }}</a></span> ({{ movie.year }}) - {{ movie.genre}}<a class="delete">X</a></li>
{% endfor %}
url code:
url(r'^(?P<movie_name>)/$', views.movie_detail, name='movie'),
Ordinarily, that exception would mean that you don't have a movie_name parameter in your movie_detail function, but clearly you do.
I'm not positive, but I think the problem may be that you don't actually have anything to match in there. For example, say you only allow movie titles with letters, numbers, and hyphens (no punctuation, spaces, etc.) Then you need to use a regular expression like this:
url(r'^(?P<movie_name>[-\w]+)/$', views.movie_detail, name='movie')
I think the problem is on the url regex. read this doc, can be helpful.
url(r'^(?P<movie_name>)/$', views.movie_detail, name='movie'),
To:
url(r'^(?P<movie_name>[\w-]+)/$', views.movie_detail, name='movie'),
Named groups¶
The above example used simple, non-named regular-expression groups
(via parenthesis) to capture bits of the URL and pass them as
positional arguments to a view. In more advanced usage, it’s possible
to use named regular-expression groups to capture URL bits and pass
them as keyword arguments to a view.
In Python regular expressions, the syntax for named regular-expression
groups is (?Ppattern), where name is the name of the group and
pattern is some pattern to match.
You might want to change the movie name in the url for a slug field, slugs are specially suited fields to convert arbitrary strings into valid url strings
as for your problem, your url is not getting properly matched, try this:
url(r'^(?P<movie_name>[-\w]+)/$', views.movie_detail, name='movie'),

NoReverseMatch with Django when the URL exists

In a template, I have {% url "news.views.article" article=article.id %} where article.id is the ID of an article currently being displayed. My urls.py contains this:
url(r'^news/$', 'news.views.index'),
url(r'^news/article/(?P<article>\d{1,4})/$', 'news.views.article'),
However, when I load the page containing the above templatetag, I get this:
NoReverseMatch at /news/
Reverse for '"news.views.article"' with arguments '()' and keyword arguments '{'article': 2}' not found.
Try it without the quotes around your view definition.
{% url news.views.article article=article.id %}
You've already defined the view name, so this should work by calling the function by name.
The syntax with quotes around the view name works only for Django 1.5. Since you're probably using an older version, you should remove the quotes, or add in your template:
{% load url from future %}
and then use the quotes.
More info on the deprecation - Django 1.3 release notes

Django url template confusion

Okay I am having a bit of an issue.
I want to create a button with a link, and right now I am using action={% url views.contest_overview %} in hopes that the reverse lookup by Django will match (r'^category/$', views.contest_overview), in my urls.py. However, this is not working and I can't figure out the proper nomenclature, despite numerous guesses.
The error I get (with my best guess above) is:
Caught NoReverseMatch while rendering: Reverse for
'views.contest_overview' with arguments '()' and keyword arguments
'{}' not found.
Thank you very much for your time!
Use the application name in the url tag, e.g. {% url myapp.views.contest_overview %}
This is what I usually do; I give names to my url. For example:
url(r'^account/register/$', 'someapp.views.register_view', name='account_register'),
Therefore in template, I can do this:
{% url account_register as url_acc_register %}
<html>
..
..
Some link

Adding two IDs to my urls dispatcher

I'm relatively new to python/django. I'm having an issue with sending to IDs through my urls.py.
I am trying to add an admin to a business profile page in my project.
My views.py:
#login_required
def make_admin(request, bus_id, user_id):
user = request.user
u = get_object_or_404(User, pk = user_id)
b = get_object_or_404(Business, pk = bus_id)
b.admin.add(u)
followcount = b.followers.count()
photo = BusinessLogo.objects.all().filter(business_link = bus_id)[:1]
return render_to_response('business/followers.html',
{'user':user, 'b':b, 'followcount':followcount, 'photo':photo, 'u':u}, context_instance=RequestContext(request))
In my template I am trying to pass the bus_id as well as the user_id but I keep getting a syntax error, which I am assuming is related to my urls.
My template:
...
{% if follow in b.admin.all %}
[Remove Admin]
{% else %}
[Make Admin]
{% endif %}
...
My urls.py at the moment:
url(r"^make/(?P<bus_id>\d+)/(?P<user_id>\d+)/$", make_admin, name="make_admin"),
url(r"^remove/(?P<bus_id>\d+)/(?P<user_id>\d+)/$", remove_admin, name="remove_admin"),
I am just having a hard time figuring out how to add the user_id to my urls. The above example does not work.
Thanks everyone,
Steve
EDIT: The error Im presented with is:
Caught NoReverseMatch while rendering: Reverse for 'remove_admin' with arguments '(1L, '')' and keyword arguments '{}' not found.
The only thing i can see that seems wrong is {% if follow in b.admin.all %} there's no follow variable in your context in the code you posted.
If you posted more details of your error, or the stack trace, it would be most helpful.
EDIT: Ok, your error is helpful :)
Caught NoReverseMatch while rendering: Reverse for 'remove_admin' with arguments '(1L, '')' and keyword arguments '{}' not found.
This means that the url reversal function got two arguments 1L and ''.
1L i just the integer 1 as a python long integer, the '' means that you passed in None or a blank string.
Since you called the url reversal in your template with {% url remove_admin b.id u.id %} the second argument is the value of u.id. Check the value of the u variable, it seems to not have a valid id attribute, so it's probably not what you expect (I'd guess it's not a User object at all)
You're not referencing the user object in the way you pass it to the context - you pass it as user, but in the template you use u.id.

Categories

Resources