I'm trying make hyperlink to next page. But I get error.
Reverse for 'show_units_list' with arguments '()' and keyword arguments '{}' not found.
Please check my code.
view.py
def units_list(request):
units_list = unit.objects.all()
return render_to_response(
'portal/units.html',
{'units': units_list},
context_instance=RequestContext(request)
)
urls.py
urlpatterns = patterns('',
# Main web portal entrance.
(r'^$', views.portal_main_page),
url(r'^portal/', views.units_list, name='show_units_list'),
)
html
<a href="?page={% url show_units_list %}">
Thanks
You forget app_name,
url(r'^portal/', app_name.views.units_list, name='show_units_list'),
units_list is part an app "portal"
Related
I am trying to understand why I am seeing a NoReverseMatch error when trying to use Django Contact Form.
The error occurs when I add a link using the following syntax to index.html:
<h3>Contact</h3>
If I use the following hardcoded syntax then no errors occur and the link to the contact form from index.html works as expected.
<h3>Contact</h3>
What I am trying to achieve is similar to what is shown the Django tutorial about removing hard coded urls.
The full error I see is:
NoReverseMatch at /index
Reverse for 'contact' with arguments '()' and keyword arguments '{}'
not found. 0 pattern(s) tried: []
In case of need, my abbreviated urls.py is:
urlpatterns = patterns('',
...
url(r'index$', views.index, name='index'),
...
url(r'^contact/', include('contact_form.urls')'),
)
I know I am missing something obvious!
It's because there is no url with name contact.
url(r'^contact/', include('contact_form.urls')'),
is url that will map all urls starting with contact to the contact_form.urls. Official docs don't say how to access contact view, but with basic understanding of django we can do something like this:
urlpatterns = patterns('',
...
url(r'index$', views.index, name='index'),
...
url(r'^contact/', include('contact_form.urls', namespace='contacts')),
)
and the in the template:
<h3>Contact</h3>
contact_form url name is found in the source code of the module.
when you use {% url 'contact' %} in template, the 'contact' is actually the name of route. In your url patterns there is no route with this name. You should include something like this into your contact_forms.urls.py:
url(r'$', views.index, name='contact_index')
Also, you should change "contact/" pattern to:
url(r'^contact/', include('contact_form.urls', namespace='contacts'))
and then use this when creating link in template:
{% url 'contacts:contact_index' %}
I have a Django project where I want to use an app for all the sites web pages. My project looks like this:
project
src
project
urls.py
views.py
...
web
migrations #package
urls.py
views.py
...
templates
web
index.html # I want this to be my root page
page2.html # This is the second page I'm trying to link to
I am trying to create a link in index.html that will take me to page2.html. This is what I am doing
In project->urls.py I have url(r'^$', include('web.urls', namespace="web")),. This should direct all page requests going to url http://127.0.0.1:8000/ to page index.html
project->views.py is empty because I want all web pages served up by the web app.
In web->urls.py I have url(r'^$', views.index, name='home_page') which relates to web->views.py and the function
def index(request):
print("Main index Page")
return render(request, 'web/index.html', {})
Which returns the correct page.
This works fine until I add the link to index.html for page2.html. The link looks like this: {% url 'web:page2' %}. I update web->urls.py. I add the following function into web->views.py:
def page2(request):
print("Page2")
return render(request, 'web/page2.html', {})
Now I get
Reverse for 'page2' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$page2/?$']
With the '{% url 'web:page2' %}' line highlighted.
When I remove the link everything works fine. What is wrong with my logic/setup?
You need to add an additional URL pattern:
urls = [
url(r'^page2/$', views.page2, name='page2'),
url(r'^$', views.index, name='home_page'),
]
or alternatively, pass a parameter you can use to identify the page to be rendered to the view. Currently, you aren't mapping a URL to the function you want to call when the URL is matched for page2, just the home page.
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 have the following setup:
/landing_pages
views.py
urls.py
In urls.py I have the following which works when I try to access /competition:
from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^competition$', 'landing_pages.views.page', {'page_name': 'competition'}, name="competition_landing"),
)
My views.py has something like this:
def page(request, page_name):
return HttpResponse('ok')
Then in a template I'm trying to do this:
{% load url from future %}
<a href="{% url 'landing_pages.views.page' page_name='competition'%}">
Competition
</a>
Which I apparently can't do:
Caught NoReverseMatch while rendering: Reverse for 'landing_pages.views.page' with arguments '()' and keyword arguments '{'page_name': u'competition'}' not found.
What am I doing wrong?
You ask in your comment to DrTyrsa why you can't use args or kwargs. Just think about it for a moment. The {% url %} tag outputs - as the name implies - an actual URL which the user can click on. But you've provided no space in the URL pattern for the arguments. Where would they go? What would the URL look like? How would it work?
If you want to allow the user to specify arguments to your view, you have to provide a URL pattern with a space for those arguments to go.
{% url [project_name].landing_pages.views.page page_name='competition' %}
Or better
{% url competition_landing 'competition' %}
I have looked a lot on google for answers of how to use the 'url' tag in templates only to find many responses saying 'You just insert it into your template and point it at the view you want the url for'. Well no joy for me :( I have tried every permutation possible and have resorted to posting here as a last resort.
So here it is. My urls.py looks like this:
from django.conf.urls.defaults import *
from login.views import *
from mainapp.views import *
import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^weclaim/', include('weclaim.foo.urls')),
(r'^login/', login_view),
(r'^logout/', logout_view),
('^$', main_view),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
#(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': '/home/arthur/Software/django/weclaim/templates/static'}),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),
)
My 'views.py' in my 'login' directory looks like:
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from django.contrib import auth
def login_view(request):
if request.method == 'POST':
uname = request.POST.get('username', '')
psword = request.POST.get('password', '')
user = auth.authenticate(username=uname, password=psword)
# if the user logs in and is active
if user is not None and user.is_active:
auth.login(request, user)
return render_to_response('main/main.html', {}, context_instance=RequestContext(request))
#return redirect(main_view)
else:
return render_to_response('loginpage.html', {'box_width': '402', 'login_failed': '1',}, context_instance=RequestContext(request))
else:
return render_to_response('loginpage.html', {'box_width': '400',}, context_instance=RequestContext(request))
def logout_view(request):
auth.logout(request)
return render_to_response('loginpage.html', {'box_width': '402', 'logged_out': '1',}, context_instance=RequestContext(request))
and finally the main.html to which the login_view points looks like:
<html>
<body>
test! logout
</body>
</html>
So why do I get 'NoReverseMatch' every time?
*(on a slightly different note I had to use 'context_instance=RequestContext(request)' at the end of all my render-to-response's because otherwise it would not recognise {{ MEDIA_URL }} in my templates and I couldn't reference any css or js files. I'm not to sure why this is. Doesn't seem right to me)*
The selected answer is out of date and no others worked for me (Django 1.6 and [apparantly] no registered namespace.)
For Django 1.5 and later (from the docs)
Warning
Don’t forget to put quotes around the function path or pattern name!
With a named URL you could do:
(r'^login/', login_view, name='login'),
...
logout
Just as easy if the view takes another parameter
def login(request, extra_param):
...
login
Instead of importing the logout_view function, you should provide a string in your urls.py file:
So not (r'^login/', login_view),
but (r'^login/', 'login.views.login_view'),
That is the standard way of doing things. Then you can access the URL in your templates using:
{% url login.views.login_view %}
Make sure (django 1.5 and beyond) that you put the url name in quotes, and if your url takes parameters they should be outside of the quotes (I spent hours figuring out this mistake!).
{% url 'namespace:view_name' arg1=value1 arg2=value2 as the_url %}
link_name
The url template tag will pass the parameter as a string and not as a function reference to reverse(). The simplest way to get this working is adding a name to the view:
url(r'^/logout/' , logout_view, name='logout_view')
I run into same problem.
What I found from documentation, we should use namedspace.
in your case {% url login:login_view %}
Judging from your example, shouldn't it be {% url myproject.login.views.login_view %} and end of story? (replace myproject with your actual project name)