Django url handling? - django

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.

Related

How to get URL without required parameters

I have the following path in my URLS:
path('remove_order/<int:order_id>', RemoveOrder.as_view(), name='md_remove_order')
What I would like to do is get the URL WITHOUT specifying an order_id in my template. Like this:
{% url 'md_remove_order' %}
so it gives me something like this:
"remove_order/"
I will then pass this value to my Javascript where the order_id will be added dynamically.
You can use re_path
re_path('remove_order(?:/(?P<pk>[0-9]+))?/$', RemoveOrder.as_view(),name='md_remove_order')
So it will both work for remove_order/ and remove_order/123/
An easy way to do that is to create another url that points to the same view but without the parameter:
urlpatterns = [
path('remove_order/<int:order_id>', RemoveOrder.as_view(), name='md_remove_order'),
path('remove_order/', RemoveOrder.as_view(), name='md_remove_order_without_params'),
]
Then use that new url
{% url 'md_remove_order_without_params' %}

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 %}">

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 urls with the same name but different regexps

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.