S Mev
S Mev

Reputation: 327

Django template {%url %} link formatting

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

Upvotes: 0

Views: 114

Answers (2)

Даниял
Даниял

Reputation: 151

Try please this syntax {% url 'some-url-name' arg arg2 as the_url %}

Upvotes: 1

taha maatof
taha maatof

Reputation: 2165

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

Upvotes: 1

Related Questions