I have 2 links in my html templates. First link pass only 1 parameter to URL and second link pass 2 parameters. Like this:
{{categ_name}}
{{subcateg_name}}
Now when i click on link with 1 parameter it works fine. I get the parameter value in my django view. But When i click on link with two parameters i only get the first parameter. I get None in the value of second parameter.
My urls.py:
urlpatterns = patterns('',
url(r'^products/(?P<categ_name>\w+)/', views.products, name='products_category'),
url(r'^products/(?P<categ_name>\w+)/(?P<subcateg_name>\w+)/', views.products, name='products_subcategory'),
url(r'^logout/',views.logoutView, name='logout'),)
My views.py:
def products(request, categ_name=None, subcateg_name=None):
print categ_name, subcateg_name
...
How to get the value of second parametre?
Change your urls to:
urlpatterns = patterns('',
url(r'^products/(?P<categ_name>\w+)/$', views.products, name='products_category'),
url(r'^products/(?P<categ_name>\w+)/(?P<subcateg_name>\w+)/$', views.products, name='products_subcategory'),
url(r'^logout/',views.logoutView, name='logout'),)
Then, you avoid your 2-parameter url being matched by the first 1-parameter pattern. The $ special character means end of string.
Related
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.
I was reading the thread Django optional url parameters
And following the steps to generate a URL with a single optional parameter.
Well, my URL should be:
/client/
/client/?clientname=John
And I have defined two urlpatterns
url(r'^$', views.index, name='index'),
url(r'^/(?P<clientname>\d+)/',views.index),
Well, at this point both of them render the page.
But, in my view:
def index(request, clientname='noparameter'):
print("The searched name is: " + str(clientname))
The searched name is always noparameter
Am I doing something wrong?
Url you are having is
/client/John/
instead of
/client/?clientname=John
also even in the following example using John will fail as your regex is for digits , check out more on topic of django dispatcher
/client/4/
if you want to get GET parameters instead you can do that in view by using the following
request.GET.get('clientanme', None)
It seems as though you are getting confused between a keyword argument and a get request. Using keyword arguments, which your urls.py is configured for, your view would like this:
def index(request, **kwargs):
clientname = kwargs.get("clientname", "noparameter")
print("The searched name is: " + str(clientname))
Your urls.py would also have to change to this for the url to this:
url(r'^client/(?P<clientname>\w+)/',views.index),
This could be called in the browser like:
/client/John
I'm using an App that configures some Admin URLs as:
#admin.py....
def get_urls(self):
urls = super(FormAdmin, self).get_urls()
extra_urls = [
url("^(?P<form_id>\d+)/entries/$",
self.admin_site.admin_view(self.entries_view),
name="form_entries"),
#......
]
return extra_urls + urls
I'm having trouble using template tags to get a URL corresponding to that, in one of my templates. I'm trying stuff like:
4-Entries
(Where forms is the App's label). I keep running into the No Reverse Match type of errors:
NoReverseMatch at /polls/ Reverse for 'forms_form_entries' with
arguments '(4,)' and keyword arguments '{}' not found. 0 pattern(s)
tried: []
What am I missing that makes the tag work correctly?
Try this in html
4-Entries
Your url pattern name form_entries doesn't match forms_form_entries from the URL tag. Django does not automatically prefix the pattern name with <app_name>_ as you appear to be expecting.
Change one of them so that they match.
I have two url patterns in Django:
urlpatterns += patterns('',
url(r'^(?P<song_name>.+)-(?P<dj_slug>.+)-(?P<song_id>.+)/$', songs.dj_song, name='dj_song'),
url(r'^(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$', songs.trending_song, name='trending_song'),
)
When I visit a URL of the first pattern, it opens it correctly. However if I try and visit a URL of the second pattern, it tries to access the first view again. The variables song_name, dj_slug, artist_slugare strings and song_id is an integer.
What should be the URL patterns for such a case with similar URL structure?
Both urls use the same regex. I removed the group names and get:
url(r'^(.+)-(.+)-(.+)/$', songs.dj_song, name='dj_song'),
url(r'^(.+)-(.+)-(.+)/$', songs.trending_song, name='trending_song'),
Of course django uses the first match.
You should use different urls for different views. For example add the prefix to the second url:
url(r'^trending/(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$',
songs.trending_song, name='trending_song'),
how can two URL point to the same function, one with parameter and one without, both should show the index page, second one pass a parameter?
But the second one is incorrect, it gives me an error when i try to pass a parameter.
(r'^$', 'index'),
(r'^/(?P<jobtype>.*)/$', 'index'),
Thanks in Advance
the whole url :
urlpatterns+= patterns('job.views',
url(r'^$', 'index'),
(r'/(?P<jobtype>.*)/$', 'index'),
(r'^profile/addJob/$', 'addJob'),
(r'editjob/(?P<jobid>.*)/$', 'editJob'),
)
the error is
Page not found (404)
Request Method: GET
Request URL: 127.0.0.1:8000/reng%C3%B8ring/
I try to pass a string paramter "rengøring"
You can make the jobtype parameter of the index view optional:
def index(request, jobtype=None):
...
if jobtype is not None:
do_something()
...
return render_to_response('index.html', locals())