i tried to add a url in django template which is supposedly to log out a user
<div>logout</div>
but it threw a NoReverseMatch
the template:
<header class="grid-header header">
<div class='logo-text'>Nitro fleet</div>
<div class="profile-detail">
{% if user.is_authenticated %}
<img class="profile-img" src="{{user.picture.url}}" />
<div>
<div>{{user.username}}</div>
<div>logout</div>
</div>
{%endif%}
</div>
</header>
the url of the app (accounts) that has the logout view:
from django.urls import path
from . import views
appname = 'accounts'
urlpatterns = [
path('register', views.register_view , name='user_register'),
path('login', views.user_login , name='user_login'),
path('logout', views.user_logout , name='user_logout'),
]
the view function:
def user_logout(request):
logout(request)
return HttpResponseRedirect(request.path_info)
the complete error log:
NoReverseMatch at /maintenance/
Reverse for 'accounts.views.user_logout' not found. 'accounts.views.user_logout' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/maintenance/
Django Version: 3.2.9
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'accounts.views.user_logout' not found. 'accounts.views.user_logout' is not a valid view function or pattern name.
Exception Location: C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: C:\Users\PC\AppData\Local\Programs\Python\Python310\python.exe
Python Version: 3.10.0
Python Path:
['C:\\Users\\PC\\Desktop\\nitrofleet',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\DLLs',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\lib',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310',
'C:\\Users\\PC\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
Server time: Mon, 31 Jan 2022 20:43:51 +0000
Just use the name of the view:
{% url 'accounts:user_logout' %}
Related
I'm working on a basic Django app (just learning, this is my third small project), and I'm trying to add links to allow a list of every post a user has made. I've got the URLS being created OK (domain/posts/username), and I want this to link to a view (that I've already created, and which works when the parameters I want to pass are hard-coded to a single value), passing the positional arguments to the view. However, I'm running aground on the RegEx / Urls part of things. I think this is the relevant info:
urls.py:
urlpatterns = [
url(r'^create/', views.create, name='create'),
url(r'^(?P<pk>[0-9]+)/upvote', views.upvote, name='upvote'),
url(r'^(?P<pk>[0-9]+)/downvote' , views.downvote, name='downvote'),
url(r'^userposts/(?P<user>[.]+)', views.userposts, name='userposts')
views.py
def userposts(request,user):
posts = models.Post.objects.filter(url__contains=user)
return render(request, 'posts/userposts.html', {'posts': posts})
home.html
a href="{{ post.url}}">{{ post.title }}</a><br/>{{ post.pub_date_pretty }} by
{{ post.author.username }}</td>
With the (?P[.]+) part of the last url line removed, the page will render OK, but the links don't work (as there's no user parameter being passed). With the regex present (not only this one, but the many others I've tried), I get a NoReverseMatch error:
NoReverseMatch at /
Reverse for 'userposts' with no arguments not found. 1 pattern(s) tried: ['posts/userposts/(?P<user>[.]+)']
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.11
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'userposts' with no arguments not found. 1 pattern(s)
tried: ['posts/userposts/(?P<user>[.]+)']
Exception Location: /usr/local/lib/python3.5/dist-
packages/django/urls/resolvers.py in _reverse_with_prefix, line 497
Python Executable: /usr/bin/python3
Python Version: 3.5.2
Python Path:
['/home/darren/redditclone/redditclone',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/home/darren/.local/lib/python3.5/site-packages',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Thu, 18 May 2017 18:21:11 +0000
Your userposts is defined like
url(r'^userposts/(?P<user>[.]+)', views.userposts, name='userposts')
which means it requires a user argument to resolve.
To pass such an argument, you'd do
{% url 'posts:userposts' user_goes_here %}
Try this instead
{{ post.author.username }}
This question already has answers here:
What is a NoReverseMatch error, and how do I fix it?
(6 answers)
NoReverseMatch exception while resetting password in django using django's default views
(1 answer)
Closed 5 years ago.
I am working on password reset functionality in Django using custom templates.
But when I entered mail:id and clicking on reset password I'm facing "No Reverse match error".Please someone help me,I understand this is an old problem, I have followed other answers and tried solving but could not come up with solution.Please find the code below and help me solve this problem.
urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from django.contrib.auth.views import password_reset,password_reset_done,password_reset_confirm,password_reset_complete
from surveysite.views import home,about,login,loggedin,loggedout
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^about/$',about, name='about'),
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^accounts/login/$', login, name='login'),
url(r'^accounts/loggedin/$',loggedin, name='loggedin'),
url(r'^accounts/logout/$', loggedout, name='logout'),
url(r'^accounts/password_reset/$', password_reset,
{'post_reset_redirect' : '/accounts/password_reset/mailed/'},
name="password_reset"),
url(r'^accounts/password_reset/mailed/$',password_reset_done),
url(r'^accounts/password_reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',password_reset_confirm,
{'post_reset_redirect' : '/accounts/password_reset/complete/'},name="password_reset_confirm"),
url(r'^accounts/password_reset/complete/$',password_reset_complete),
url(r'^admin/', admin.site.urls),
]
Password_reset_email
{% autoescape off %}
You're receiving this email because you requested a password reset for your user account at {{ site_name }}.
Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb36=uid token=token %}
{% endblock %}
Your username, in case you've forgotten: {{ user.get_username }}
Thanks for using our site!
The {{ site_name }} team
{% endautoescape %}
Error Stack Trace
NoReverseMatch at /accounts/password_reset/
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'MQ', u'token': u'4jz-530973903a3b600e2a97'}' not found. 0 pattern(s) tried: []
Request Method: POST
Request URL: http://localhost:8000/accounts/password_reset/
Django Version: 1.10.5
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'django.contrib.auth.views.password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb36': 'MQ', u'token': u'4jz-530973903a3b600e2a97'}' not found. 0 pattern(s) tried: []
Exception Location: C:\Python27\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 392
Python Executable: C:\Python27\python.exe
Python Version: 2.7.13
Python Path:
['G:\\Manoj\\Software Engineering-Yui Man lee\\Django_Project\\surveysite',
'C:\\Python27',
'C:\\Python27\\Tools\\Scripts',
'G:\\Manoj\\Software Engineering-Yui Man lee\\Django_Project\\surveysite',
'C:\\WINDOWS\\SYSTEM32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\PIL']
In template G:\Manoj\Software Engineering-Yui Man lee\Django_Project\surveysite\templates\registration\password_reset_email.html, error at line 6
I have tried solutions from below links but could not find the solution.
NoReverseMatch on password_Reset_confirm
https://github.com/bread-and-pepper/django-userena/issues/380
django password reset NoReverseMatch error
I'm following tutorial from http://code.techandstartup.com/django/reset-password/ ==> customized Templates.
Instead of referencing the view, you need to just reference the name of your url pattern that you are trying to match
{% url 'password_reset_confirm' uidb36=uid token=token %}
I would like to make just a page that has link to login to twitter/facebook/google with django-social-auth.
but I get a error NoReverseMatch: Reverse for '' with arguments '(u'twitter',)' and keyword arguments '{}' not found.
def index(request):
ctx = {}
return render_to_response('index_before_login.html', {}, RequestContext(request))
index_before_login.html is following
<li>Enter using Twitter</li>
urls.py is following
urlpatterns = patterns('',
url(r'^$', 'lebabcartoon.views.index'),
#url(r'^socialauth_', 'lebabcartoon.views.index'),
url('', include('social_auth.urls')),
my environment is
Django ver1.5
Python Version: 2.7.3
django-social-auth: 0.7.5
anyideas?
Wrap url name in quotes
{% url 'socialauth_begin' 'twitter' %}
To save someone using the new python-social-auth and django > 1.4
Use this :
{% url 'social:begin' 'twitter' %}
I had a similar problem, try adding the following to the url.py file in your project.
url(r'auth/', include('social_auth.urls'))
And also make sure your url parameters are wrapped in quotes like so.
{% url "socialauth_begin" "twitter" %}
I've been trying to write a custom template tag to shorten links with bitly, I've attached the code and error I've been getting below. I've tried to look into the documentation provided by Django but cannot see what it is that I am doing wrong.
I've put my templatetag in the following layout:
scribbler/
models.py
templatetags/
__init__.py
shortenlink.py
views.py
the custom tag file that I've written:
shortenlink.py
from django import template
from django.conf import settings
from urllib import urlencode
from urllib2 import urlopen
register = template.Library()
#register.simple_tag
def bitlyshort(the_url):
endpoint = 'https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}&format=txt'
req = urlencode(endpoint.format(settings.ACCESS_KEY, the_url))
return urlopen(req).read()
part of the template that uses the template tag:
template
{% load shortenlink %}
<p>{{ bitlyshort "http://www.google.com" }}</p>
error
TemplateSyntaxError at /user/sankaetp/
Could not parse the remainder: ' "http://www.google.com"' from 'bitlyshort "http://www.google.com"'
Request Method: GET
Request URL: http://localhost:8000/user/sankaetp/
Django Version: 1.4.1
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: ' "http://www.google.com"' from 'bitlyshort "http://www.google.com"'
Exception Location: /Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/django/template/base.py in __init__, line 563
Python Executable: /Users/sankaetp/virtualenvs/myproject/bin/python
Python Version: 2.7.3
Python Path:
['/Users/sankaetp/virtualenvs/myproject/bin/django_worksquid',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/djangoembed-0.1.1-py2.7.egg',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/httplib2-0.7.4-py2.7.egg',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/BeautifulSoup-3.2.1-py2.7.egg',
'/Users/sankaetp/virtualenvs/myproject/lib/python27.zip',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/plat-darwin',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/plat-mac',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/lib-tk',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/lib-old',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/lib-dynload',
'/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7',
'/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/plat-darwin',
'/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/lib-tk',
'/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/plat-mac',
'/Users/sankaetp/.pythonbrew/pythons/Python-2.7.3/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages',
'/Users/sankaetp/virtualenvs/myproject/lib/python2.7/site-packages/PIL']
Server time: Sun, 26 Aug 2012 18:54:26 -0500
The error is on this line:
<p>{{ bitlyshort "http://www.google.com" }}</p>
Template tags use {% ... %} (template filters use {{ ... }}). Try:
<p>{% bitlyshort "http://www.google.com" %}</p>
There is an alternative way to cause this error:
Instead of:
{{ data|filter }}
If you use:
{{ data | filter }} <!-- Note Spaces -->
The spaces on either side of the | character will cause an exception during page render.
I make google authorization (Oauth 2.0) with django-social-auth and getting error
Django Version: 1.3
Exception Type: TemplateSyntaxError
Exception Value:
Caught NoReverseMatch while rendering: Reverse for 'begin' with arguments '(u'google-oauth2',)' and keyword arguments '{}' not found.
Exception Location: /usr/local/lib/python2.6/site-packages/django/template/defaulttags.py in render, line 450
Python Executable: /usr/local/bin/python
Python Version: 2.6.6
Template
<a rel="nofollow" href="{% url begin "google-oauth2" %}" ><img src="{{ MEDIA_URL }}social/google.png" id="google"></a>
url.py
urlpatterns = patterns('',
url(r'', include('social_auth.urls')),
)
social_auth.urls
urlpatterns = patterns('',
url(r'^login/(?P<backend>[^/]+)/$', auth, name='begin'),
url(r'^complete/(?P<backend>[^/]+)/$', complete, name='complete'),
url(r'^associate/(?P<backend>[^/]+)/$', associate, name='associate_begin'),
url(r'^associate/complete/(?P<backend>[^/]+)/$', associate_complete,
name='associate_complete'),
url(r'^disconnect/(?P<backend>[^/]+)/$', disconnect, name='disconnect'),
)
What am I doing wrong?
Please help to find the solution.
You are on django 1.3. Have you included the new url template tag with {% load url from future %}. If you have then your syntax for the url tag is incorrect and would need to be {% url 'begin' 'google-oauth2' %}. If you haven't then your syntax is correct.
Have you ensured that your social_auth.urls is being included correctly? To do this (with debug enabled) manually go to http://yourdevserver/login/google-oauth2/ and ensure you don't see a 404 page. If you do you will be able to see what url's where attempted and that should give you direction on how to fix it.