in views.py I have this:
def logout(request,key):
auth.logout(request)
return HttpResponseRedirect(reverse('airAgency.views.index',args=[key]))
in index template i wanna when user click on a link,logout view run:
logout
I wanna pass key parameter to logout view,Imagine I have an object named agn and it's WebSite field is going to pass to logout view, s.th like this:
<a href="{% url airAgency.views.logout agn.WebSite %}">
Imagine agn.WebSite has the value of mastane above code cause this error:
Caught NoReverseMatch while rendering: Reverse for 'airAgency.views.logout' with arguments '(u'mastane',)' and keyword arguments '{}' not found.
what's the right way to do this?
Caught NoReverseMatch - usually means that the url is being called by incorrect arguments, or that something else is wrong when {% url %} tries to call the url specified.
You need to define url pattern for that view function to be called in urls.py. And in the url pattern specify a view function.
urls.py
url(r'^logout/(?P<key>[a-zA-Z0-9\-]+)/$', "airAgency.views.logout", name="logout"),
index.html
<a href="{% url logout agn.WebSite %}">
This might not work if you just copy paste it because I don't know the exact project setup.
The key is to have urls.py where regex is used to create patterns for how the url looks like, then a view function that needs to be invoked, and finally a name, which you can then use in your templates.
This is all explained in the basic Django tutorial.
Related
Error while passing value to view from template using url
Error :
Error during template rendering
In template D:\TONO\DJANGO_TASK\user_auth\templates\auth_app\other-user-listing.html, error at line 114
Reverse for 'add_to_group' with arguments '(name: Administrator,)' not found. 1 pattern(s) tried: ['auth_app\\/<str:username>/']
This my urls.py
url(r'^<str:username>/', views.add_to_group, name="add_to_group"),
This is the call from template
<i class="icon-plus">Add</i>
In your call from template see if you do this if that solves the problem, I remember I was facing this issue and this is what I did:
The syntax for specifying url is {% url namespace:url_name %}. So, check if you have added the app_name in urls.py.
Your url defines a named argument for the url "add_to_group". You should pass this argument as a keyword argument
{% url 'add_to_group' username=username %}
You are mixing the new and old style urls. To use the new style you should use path
path('<str:username>/', views.add_to_group, name="add_to_group"),
Error corrected using :
url(r'^auth_app/add_to_group/(?P<username>[-\w]+)/$', views.add_to_group, name='add_to_group'),
Django URL pattern not recognized due to tuple entry
I want to put a hyperlink in HTML code to redirect to a page with sending data with it. hyperlink path is dynamic so I used {% url "check" book.id %} in HTML code. book.id gives correct data but it appears in tuple format ('12',)
when I call particular page by writing static URL like http://127.0.0.1:8000/check/12/ it works fine "check" as view area and 12 as an argument passed
how to use a dynamic path
views.py
def add(request):
book = Readdata.objects.all()
return render(request, 'result.html', {'books': book})
def check(request,id):
return render(request,'result.html',{'result': id})
urls.py
urlpatterns = [
url('add', views.add, name='add'),
url(r'^check/(\d+)/$', views.check, name="delete_book"),
url('', views.index, name="index")
]
html
click not working
click working
error-
Reverse for 'check' with arguments '(21,)' not found. 1 pattern(s)
tried: ['check']
This has nothing to do with tuples.
As the error says, you don't have a "check" URL that takes an argument "21". The only URL that takes an argument is "delete_book". "check" takes no arguments.
Everything was working fine when i have added fobi in my project but somehow while i have added namespace in my url it start giving me the error like below.
Main urls.py
re_path(r'^formbuilders/', include('apps.form_builders.urls'),),
Form_builders urls.py
url(_(r'^$'), view=dashboard, name='fobi.dashboard'),
Template
{% url 'fobi.dashboard' as fobi_dashboard_url %}
Reverse for 'fobi.dashboard' not found. 'fobi.dashboard' is not a valid view function or pattern name.
I have visited many site but did not received any quick fix.
Could you please help me out to fix this issue?
As the title implies I've got the NoReverseMatch error, but my url.py has the corresponding named url. I feel pretty confident I've missed something simple, but being new I can't seem to find my own problem.
ERROR: (Generated in the below employee_edit.html file)
Caught NoReverseMatch while rendering: Reverse for ''employee_new''
with arguments '()' and keyword arguments '{}' not found.
\home\username\mysite\myapp\views.py:
class v_EmployeeCreate(CreateView):
model = Employee
template_name = 'employee/employee_edit.html'
def get_success_url(self):
return reverse('employee_list')
\home\username\mysite\url.py:
from myapp.views import v_EmployeeCreate, v_EmployeeList
urlpatterns = patterns('',
< ... snip ...>
url(r'^newEmployee$', v_EmployeeCreate.as_view(), name="employee_new"),
)
\home\username\mysite\myapp\templates\employee\employee_edit.html (line 7):
<form action="{% url 'employee_new' %}" method="POST">
I feel like there is a file path issue, but I'm not sure how I would resolve that. The named URL works to get me to the template, but then the template itself fails to generate a url.
For the sake of documentation so far I have:
reloaded django
checked spelling
confirmed non-template functionality (Same setup without the template tags is fine. page loads)
Working from tutorial: http://effectivedjango.com/tutorial/views.html#creating-contacts
I think you are using an old version of Django - before 1.5 (current is 1.6). The clue is that your error message has two single-quotes around the view name: in those older versions, you shouldn't put quotes around the name in the url tag.
You should (preferably) upgrade Django, or (if you really can't do that) use {% url employee_new %}
Hello? I am trying to redirect a particular user to a custom page once they log in in django. The Admin will be directed to their usual admin interface while this particular user will go to their own custom page.
I have written this code and placed it in my views
def custLogin(request):
if request.user.username == '***':
return HttpResponseRedirect('http://********************.html')
else:
return login(request,template_name='login.html')
I have pointed the accounts/login url in urls.py to custLogin as below
(r'^accounts/login/', custLogin),
I however keep getting the error
Caught NoReverseMatch while rendering: Reverse for 'django.contrib.auth.views.login' with arguments '()' and keyword arguments '{}' not found.
Any pointers please?
Maybe, you should use redirect shortcut, which returns an HttpResponseRedirect, to point your users to different places after custLogin view processed.
from django.shortcuts import redirect
def custLogin(request):
if request.user.is_staff:
return redirect('/some/admin/url')
else:
return redirect('/some/regular/user/url')
As documentation says, you can use the redirect() function in a number of ways: by passing a view name, an object or url. See documentation for more info
Note, that I use is_staff user field to determine: is user an admin or not.
For redirecting each user to his own page use reverse
First create url route which accepts parameter. It's described here
And redirect like
return HttpResponseRedirect(reverse('arch-summary', args=[1945]))
where arch-summary - named url, and in list args - parameters for it. Example here
To distinguish admin user from usual user, check it in view and redirect to different url.
In your template, you may have forgotten to do this at the beginning of your template:
{% load from future url %}
This is useful when using:
{% url 'name_of_my_url_inurl.py' %}