I've 2 urls pointing to the same place:
url(r'^index/$', 'proj.views.index'),
url(r'^index/show_closed/$', 'proj.views.index'),
If I use in a template file this:
{% url proj.views.index %}
it' ll return /index/show_closed
and this is OK, but how can i make {% url %} to return only /index ?
Use named patterns; giving your pattern a unique name:
url(r'^index/$', 'proj.views.index', name='index_direct'),
url(r'^index/show_closed/$', 'proj.views.index', name='index_closed'),
then refer to those names in the {% url %} tag instead of the view:
{% url 'index_direct' %}
{% url 'index_closed' %}
Related
When I use {% url %} method to create link
{% url application_name:staff:url.linkfield %}
it is producing error in the console i.e.
raise TemplateSyntaxError("Could not parse the remainder: '%s' "
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: ':staff:url.linkfield' from 'application_name:staff:url.linkfield'
This is my url.py
app_name="application_name"
urlpatterns=[
url(r"^staff/",include('application_name.staff_url', namespace='staff')),
url(r"^customer/",include('application_name.customer_url', namespace='customer')),
]
my staff_url.py
from application_name import views
app_name="staff"
urlpatterns=[
url(r"^customers/",views.customers, name='customers'),
url(r"^orders/$", views.orders, name='orders'),
url(r"^payments/$", views.payments, name='payments'),
]
my customer_url.py
from application_name import views
app_name="customer"
urlpatterns=[
url(r"^items/",views.items, name='items'),
url(r"^checkout/$", views.checkout, name='checkout'),
url(r"^make_payment/$", views.make_payment, name='make_payment'),
]
staf url would be staff/orders or staff/payments
customer urls would be customer/items or customer/checkout etc
Please what can i do
Update
{% for url in links %}
{% url.linkfield %}
{%endfor%}
That is what url.linkfield is for
Update to include view
View
staffLink=[
{'linkfield':"customers", 'name':"Customers",'slug':"staff"},
{'linkfield':"orders", 'name':"Orders",'slug':"staff"},
{'linkfield':"payments", 'name':"payments",'slug':"staff"}]
links=staffLink
request.session['links']= links
context_dict = {'links':links}
html template
{% for link in request.session.links %}
<a href="{% url application_name:staff: link.linkfield as the_url %}" class="nav-link">
{% endif %}
so this is your main url:
urlpatterns=[
url(r"^staff/",include('application_name.staff_url', namespace='staff')),
url(r"^customer/",include('application_name.customer_url', namespace='customer')),
]
you do not need an app_name for the main url.
to rewrite:
{% url application_name:staff:url.linkfield %}
would be
{% url 'staff:customers' %} or
{% url 'staff:orders' %} or
{% url 'staff:payments' %}
or the other urls
{% url 'staff:customers' %} or
{% url 'customer:checkout' %} or
{% url 'customer:make_payment' %}
if links is a result of a queryset, and you use it as context,
and linkfield is a field of that model you used to query, then
{% for url in links %}
{{ url.linkfield }}
{%endfor%}
Try please this syntax
{% url 'some-url-name' arg arg2 as the_url %}
i am new to django. this is the error i keep getting when i try to visit node.name page.
page not found Request URL: http://127.0.0.1:8000/Category//
urls.py
Category/<category_id>/ [name='productlisting']
index.html
{% load mptt_tags %}
<ul class="root">
{% recursetree listing %}
<li>
{{ node.name }}
</li>
{% endrecursetree %}
</ul>
urls.py
path('Category/<category_id>/',views.productlisting, name='productlisting'),
path('Category/<category_id>/product/<product_id>/',views.details, name='details'),
thanks in advance.
First you have an extra ending / in the endpoint you are trying to hit.
This is not how we name urls in anchor tag. Please refer here on how to name url and how to use it in template
so basically idea is that you would name url in urls.py in following manner:
path('Category/<int:category_id>/',views.productlisting,name='productlisting'),
then you would use this url name in template as:
{{ node.name }}
I'm doing some pagination in Django. I want to show the pages' number which are ahead of the current page. I am using the following code:
{% for page in blogs_page.paginator.page_range|slice:"0:{{ blogs_page.number }}" %}
But this seems useless; the result does the same as the following:
{% for page in blogs_page.paginator.page_range %}
The slice does not work here. How do I fix this?
Never use {{ }} inside of {% %}, don't do this {% {{ }} %}.
{% for page in blogs_page.paginator.page_range|slice:"0:blogs_page.number" %}
I think it won't work. If I were you I would create a custom tag and executed all the logic there. So, it will look like this:
Template:
{% custom_tag blogs_page as result %}
{% for page in result %}
templatetags/tags.py:
from django import template
register = template.Library()
#register.simple_tag
def custom_tag(bl_page):
return bl.page.paginator.page_range[0:bl_page.number]
Details: custom tags
I want to have my url pattern like the below pattern:
host:8000/archive/2/
I define page_kwarg in my view but I still receive: host:8000/en/2
Code form main url.py file:
url(_(r'^archive'), include('events.urls_archive', namespace='archive')),
start edit1
and link form main site to my app:
<a href="{% url 'archive:list' %}" title="{% trans 'Archive' %}">
{% trans 'Archive' %}
</a>
end edit1
start edit2
This is the url in my app urls_archive.py:
urlpatterns = [
url('^/(?P<page>\d+)/$', ArchiveListView.as_view(), name="list"),
url('^/(?P<slug>[-\w]+)/(?P<pk>\d+)$', ArchiveDetailView.as_view(), name="detail"),
]
end edit2
The code for my view:
class ArchiveListView(ListView):
model = Booking
queryset = Booking.objects.filter(period__type='archive').order_by('-date_start')
paginate_by = 80
page_kwarg = 'page'
Here is my template code:
{% if is_paginated %}
{% if page_obj.has_previous %}
<h4>Previous</h4>
{% endif %}
<span class="arrow header"><h4>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</h4></span>
{% if page_obj.has_next %}
<h4>Next</h4>
{% endif %}
{% endif %}
Please provide any help.
Thanks.
page_kwarg sets the key that the page number is passed in as. It doesn't affect how it is used in the page. For some reason, you are outputting that directly as /{{ page_obj.previous_page_number }}, which resolves to just /2. You should output it in the same format as it is passed in:
?page={{ page_obj.previous_page_number}}
Edit
If you want your page number to be specified as part of the path arguments, you should use the url tag, like any other URL:
{% url 'archive' page=page_obj.previous_page_number %}
I'm trying to build a simple flask page that displays links from a dictionary of text/links:
urls = {'look at this page': www.example.com, 'another_page': www.example2.com}
#app.route('/my_page')
def index(urls=urls):
return render_template('my_page.html',urls=urls)
My template page looks like this:
{%- block content %}
{%- for url in urls %}
{{ url }}
{%- endfor %}
{%- endblock content %}
I can't quite seem to understand how to create dynamic urls like this. The code produces this error:
TypeError: 'NoneType' object has no attribute '__getitem__'
Can anyone point out my problem or a solution?
UPDATE: Here's my updated code:
#app.route('/my_page')
def index():
context = {'urls': urls}
return render_template('index.html', context=context)
And the template:
{%- block content %}
{% for key, data in context.items() %}
{% for text, url in data.items() %}
{{ text }}
{% endfor %}
{% endfor %}
{%- endblock content %}
This solution is close, however each link get prepended with my app's url. In other words I get this:
look at this page
I just want:
look at this page
Try this instead:
urls = {
'A search engine.': 'http://google.com',
'Great support site': 'http://stackoverflow.com'
}
#app.route('/my_page')
def index(): # why was there urls=urls here before?
return render_template('my_page.html',urls=urls)
{%- block content %}
{%- for text, url in urls.iteritems() %}
{{ text }}
{%- endfor %}
{%- endblock content %}
url_for is only for building URLs with Flask. Like in your case:
print url_for('index') # will print '/my_page' ... just a string, no magic here
url_for takes an endpoint name as first parameter which is by default the name of the view function. So the endpoint name for your view function index() is simply 'index'.