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.
Related
I found a way to inject variables into the URL per Django Template: Add Variable Into URL As Parameter, but my reverse match is not working. Do I need to use re-path and use regular expressions?
Current url.py
path('student/item/drilldown/<str:type>/<int:num>/',views.StudentBehaviorListView.as_view(),name='student_item_list_drilldown'),
Original w/hard-coding:
<a href="{%url 'registration:student_item_list_drilldown' type='grade' num=6%}">
With variable injection:
{{i.grade}}th Grade
Error message:
NoReverseMatch at /registration/dashboard/
Reverse for 'student_item_list_drilldown' with no arguments not found. 1 pattern(s) tried: ['registration/student/item/drilldown/(?P<type>[^/]+)/(?P<num>[0-9]+)/\\Z']
This:
{% url 'registration:student_item_list_drilldown' %}
Will try to find path named student_item_list_drilldown before it is read as html. That's why it tries (and fails) to find <str:type> and <int:num> in {% url 'registration:student_item_list_drilldown' %}. You have to include that variables inside {% url ... %} brackets.
The best approach would be that:
{% url 'registration:student_item_list_drilldown' 'grade' i.grade %}
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' %}
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%}
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.
I am trying to build a date-based URL with Django's url template tag. I have a datetime object that I can display like so:
{{block|date:"F j Y"}}
However, when I use nearly the same syntax with the url templatetag, like so:
{% url meeting block|date:"Y" %}
I get an error -- it appears that the only thing passed to url is an empty string:
... Reverse for 'meeting' with arguments '(u'',)' and arguments ...
What might I be doing wrong?
The url tag is a bit strange, and is very picky about its arguments. In particular, I don't think it evaluates any filters in its arguments.
You could try this:
{% with block|date:"Y" as blockyear %}{% url meeting blockyear %}{% endwith %}