How to get URL without required parameters - django

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

Related

Django URL with parameter not working with include

I am trying to pass a parameter to a url before my include statement. I think my problem should be similar to this one, but I can't figure out why mine isn't working. I get the error NoReverseMatch at /womens/ Reverse for 'shampoo' with arguments '('womens',)' not found. 1 pattern(s) tried: ['(?P<gender>womens|mens)/items/shampoo/$']
*config.urls*
urlpatterns = [
re_path(r'(?P<gender>womens|mens)/items/$, include('items.urls')),
...
]
*items.urls*
urlpatterns = [
path('shampoo', views.shampoo, name='shampoo'),
path('soap', views.soap, name='soap'),
...
]
{% url 'items:shampoo' gender='male' %}
I expect to get male/items/shampoo, but it doesn't work. Despite this, I can still manually go to male/items/shampoo.
This might work when you do it manually like male/items/shampoo, but as it is giving a NoReverseMatch error, Django is not able to find a matching URL pattern that you provided which included the parameters.
After looking into this for some time, something like this might be happening when you use {% url 'items:shampoo' gender='male' %}
Assuming you are running this on your localhost, you expect the URL to look like male/items/shampoo
This url says, look into male section, among items show shampoo.
But you are passing the parameter in the wrong place. {% url 'items:shampoo' gender='male' %} means that the parameter male is passed to a url which has a namespace of shampoo. The URL with the namespace of shampoo does not accept any parameters.
Maybe try changing your code as follows.
*config.urls*
urlpatterns = [
re_path(r'^$', include('items.urls')),
...
]
from django.conf.urls import url, path
*items.urls*
urlpatterns = [
url(r'^(?P<gender>\w+)/items/shampoo/$', views.shampoo, name='shampoo'),
path('soap', views.soap, name='soap'),
...
]

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

How do include a url from my urlconf in a template

the urls.py is:
url(r'^paypal',
ListView.as_view(
model=PaypalMail,
context_object_name='paypal_email_links',
template_name='tools/payment.html')),
I want to make a url in template
{% url xxxx %}
how to write it ?
Firstly add a name to your url -
url(r'^paypal', ListView.as_view(model=PaypalMail,
context_object_name='paypal_email_links',
template_name='tools/payment.html'),
name = 'paypal_view'),
Then construct your reverse function -
{% url 'paypal_view' %}
Have read of the docs on naming url patterns.

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.

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.