Django urls with the same name but different regexps - django

I have these types of urls:
/city/
/city/category/
/city/category/subcategory/
All of the urls are handling by one view. Is there a way to use the same name for these urls to subsequently use tags like these:
{% url 'city_index' city.slug %},
{% url 'city_index' city.slug category.slug %},
{% url 'city_index' city.slug category.slug subcategory.slug %}
I tried this:
url(r'^(?P<city_slug>[\w\-]+)/$',
'real_city_index',
name='city_index'
),
url(r'^(?P<city_slug>[\w\-]+)/(?P<category_slug>[\w\-]*)/{0,1}$',
'real_city_index',
name='city_index'
),
But in that case the second url reverse returns url without trailing slash.
If you do not write /{0,1}, urls like /city/category won't work, that is worse than reversing without slash.

Is there a good reason not to call them 'city_index', 'city_index_with_category' and 'city_index_with_sub_category'?

Django has named URL patterns for this. The problem with your code is that you use city_index for all of the url's name properties. Change these to name='city_index', name='city_category' and name='city_subcategory' and all should be well. The view name (real_city_index) can be the same for all url patterns.

Related

How to replace re "." in Django template url tag

I have Django url routes that looks like this:
app_name = 'courses'
urlpatterns = [
url(r'./(?P<courseid>[0-9]+)/$', views.viewcourse, name='view_course'),
url(r'./(?P<courseid>[0-9]+)/review/$', views.reviewcourse, name='review_course')
]
The "." in the regular expression will usually be replaced by a slug, e.g.:
courses/computer-science/3/
Now I need a link in my 'view course' template to take the user to the review page, i.e:
courses/computer-science/3/review/
I could do this by simply appending review to the current url:
{{ request.path }}review
However, I would rather do it the 'correct' way using a template url tag
{% url 'courses:review_course' courseid=course.pk %}
However, this produces:
courses/30/review, which of course fails.
Is it possible to generate the correct link using the {% url %} tag without having to change the "." into a name capture parameter?
Change your urls.py :
url(r'(?P<course_type>[a-z_-]+)/(?P<courseid>[0-9]+)/$', views.viewcourse, name='view_course')
Now in your view you have access to course_type in your kwargs and you can then load whatever you want (as I have no code, i'm guessing what you're doing :)).
And in your template:
{% url 'courses:review_course' courseid=course.pk course_type=something%}

Could not parse the remainder: '/{{menu.Info.page}}' from ''item'/{{menu.Info.Page}}'

<img id="page" class="abc" src="{{STATIC_URL}}page/code_251.png" style=""/>
Hitting this url like localhost:8000/app/page works fine.
If I want something from views and append that pageid with url then showing error as Couldn't parse.
From views:{{page.pageid}}
output url should be :localhost:8000/app/?pageid=xx
for this i tried with below syntax:
<img id="page" class="abc" src="{{STATIC_URL}}page/code_251.png" style=""/>
But above syntax did't worked for me.
urls.py
url(r'^(page)(?:/(?P<page_id>[0-9]+))?/$',page_ViewDetails_TemplateView.as_view(),name="page"),
May be some changes need to be done on urls.py as well.
Can someone share some idea!!
You're confused about at least two things here.
Firstly, you would never use {{ }} inside a tag. You're already in the template language context there: you have access to variables directly.
Secondly, the {% url %} tag works on urlpattern names and parameters, not literal URLs. And your page URL does not expect a querystring value for page_id: it expects it as part of the path. Your generated URL needs to be "/page/3", not "/page?page_id=3".
So your URL tag is just:
<a href="{% url 'page' page_id=page.pageid %}">

Django : model instance url is appending to the current url

i have some link in a template to which i want to point to particular url.
template is accessed at url : loacalhost:8000/account/profile
{% for poll in voted_poll_list %}
<h4>{{ poll.title }} </h4>
{% endfor %}
in models.py i have created the url for the poll objects to be used in a template.
def link_url(self):
return "polls/"+ "allcat/" +str(self.id)
the problem is when a link in template is clicked it is point to the loacalhost:8000/account/profile/polls/allcat/1 instead of loacalhost:8000/polls/allcat/1 which matches to url pattern
url(r'^polls/(\w+)/(?P<pid>[-\d]+)/', 'pollsite.views.poll_detail', name='detail_poll'),
the problem is link url of object is appended to current url. how can i avoid this ?
#garnertb 's solution works, but you should probably look into using the reverse function rather than hardcoding your url.
Something like:
return reverse('detail_poll', self.id)
This will not only take care of the leading slash, but also avoid trouble if you ever start changing your url configuration.
Try leading the url with a forward slash:
def link_url(self):
return "/polls/allcat/" +str(self.id)

Getting the root url in Django

In my view, I want to make a request to mine.com/more/stuff/ from an arbitrary page such as mine.com/lots/of/stuff/to/use or from mine.com. Thus, I can't make this a relative request using ./ or ./../ type things. Do I have to use a context processor to do {{URL_BASE}}more/stuff/? Is there a set way to do this in Django or a best way?
why don't you use named urls? it's always works.
for example {% url 'admin:index' %} always printed as url to admin(in case if you using default django.contrib.admin app).
if you'll have in urls.py smth like
url(r'^lots/', Lots.as_view(), name='lots'),
then just use smth like
{% url 'lots' %}
Don't hardcode your urls!
Instead of a relative url, use an absolute url: /
If you're on mine.com/lots/of/stuff/to/use or mine.com, hitting a link with url: /foo/ will both go to mine.com/foo/

Django url handling?

After decoupling url file to our app we are facing problem:
Example:
http://www.oursite.com/ourprefix/xyz/wsz
How to handle urls in template ( to accomodate for any prefix(ourprefix) in url)
How to do HttpResponseRedirect without hard-coded urls (also outprefix problem is present here)
Use named urls in urls.py.
Use the {% url name %} template tag. It will insert the correct path.
Use reverse('name', **kwargs) for the redirect.
an example:
in proj/urls.py:
patterns = patterns('',
(r'^prefix/', include('proj.app.urls') ),
)
in proj/app/urls.py:
patterns = patterns('',
url(r'object/^(?P<pk>\d+)/edit/', edit_object_view, name="edit"),
)
in proj/app/views.py:
return HttpResponseRedirect(reverse('app:edit', {'pk':pk}))
in proj/app/templates/app/my_template.py:
<a href="{% url app:edit pk=pk %}"> <!-- generates /prefix/object/123/edit/ -->
If I understand you right, you want to resolve a particular view to a URL inside the template?
You should use the url-reverse method in Django. See here.
1) For the template, you can use:
Link
Where the "prefix" is a variable set in your Context that you pass to the template. You can also dynamically pick the right URL:
{% url application.views.viewfunc parameter1 parameter2 %}
See here for more details.
2) So to HttpResponseRedirect, you can do:
HttpResponseRedirect(reverse(your_view_function))
It also accepts parameters.