mrg
mrg

Reputation: 53

Using array elements as relative URLs in a django template

I am totally new to django, and have been struggling with the following issue for a while. I am sending a sorted list of lists to a template from views.py to a template, in which each sublist (containing two elements) should be displayed. I would like the first element to be a link, in such a way that the String content should be the relative URL to be displayed.

So I used the following syntax:

<a href="{% url {{user.0}} %}">{{user.0}}</a>

but it gives me the following error:

TemplateSyntaxError at /app/best_travelled/
Could not parse the remainder: '{{user.0}}' from '{{user.0}}'

Please find below the relevant code snippets:

views.py:

def best_travelled(request):

    trips = Trip.objects.all()
    user_trips={}
    for trip in trips:
        user_trips[trip.owner] = user_trips.get(trip.owner,0)+1

    user_list = []
    for key, value in sorted(user_trips.items(), key = itemgetter(1), reverse = True):
        user_list.append([key, value])  
    context_dict = {'user_list':user_list}
    return render(request,'best_travelled.html',context_dict)

Template:

<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Most Recent Trips - Travelmate</title>
</head>

<body>
  <h1>Best-Travelled</h1>
  <div>
    {% if user_list %}
    <ol>
      {% for user in user_list %}
      <a href="{% url {{user.0}} %}">{{user.0}}</a>
      <li>{{user.0}}, Number of trips: {{user.1}}</li>

      {% endfor %}
    </ol>
    {% else %} There are no travellers yet! This is your chance to become the very first one! <br /> {% endif %}


    <a href="{% url 'add_trip' %}">Add a New Trip</a>
  </div>
  <div>
    <ul>
      <li><a href="{% url 'login' %}">Login</a></li>
    </ul>
  </div>



</body>

</html>

And for the profiles:

views.py

@login_required
def view_profile(request,username):


    user = get_object_or_404(User, username=username)
    trips = Trip.objects.filter(owner=request.user)
    context_dict = {'user':user, 'trips':trips }

   return render(request,'view_profile.html',context_dict)

app/urls.py

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^about/$', views.about, name='about'),
    url(r'^contact/$', views.contact, name='contact'),
    url(r'^pop_trips/$', views.pop_trips, name='pop_trips'),
    url(r'^recent_trips/$', views.recent_trips, name='recent_trips'),
    url(r'^best_travelled/$', views.best_travelled, name='best_travelled'),
    url(r'^most_active_travellers/$', views.contact, name='most_active_travellers'),

    url(r'^passport/$', views.passport, name='passport'),
    url(r'^add_trip/$', views.add_trip, name='add_trip'),

    url(r'^settings/$', views.settings, name='settings'),
    url(r'^my_trips/$', views.my_trips, name='my_trips'),
    url(r'^(?P<username>[\w\-]+)/$', views.view_profile, name='view_profile')
]

project/urls.py

urlpatterns = [
    url(r'^$', app_views.home, name='home'),
    url(r'^admin/', admin.site.urls),
    url(r'^login/$', auth_views.login, name='login'),
    url(r'^logout/$',auth_views.logout,name='logout'),
    url(r'^oauth/',include('social_django.urls',namespace='social')),
    url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
    url(r'^accounts/',include('registration.backends.simple.urls')),
    url(r'^app/', include('app.urls')),
    url(r'^(?P<username>[\w\-]+)/$', app_views.view_profile, name='view_profile'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Thanks heaps to anyone who can help!

Upvotes: 0

Views: 614

Answers (2)

Catalin
Catalin

Reputation: 135

It should be:

<a href="{% url 'url_name' arg_1 arg_2 %}">{{user.0}}</a>

Considering you have:

url(r'my-url/(?P<arg_1>[0-9a-f-]+)/(?P<arg_2>[a-f-]+)/$', MyClassView.as_view(), name="url_name"),

Where:

  • url_name is the name of your url
  • arg_1 and arg_1 are your arguments for your url(if there is the case, if not stays without them).

like: <a href="{% url 'url_name' %}">{{user.0}}</a>

Side-note: Once you are {% here %} all the context pushed is available without any extra {{ and }}

Upvotes: 1

Antoine Pinsard
Antoine Pinsard

Reputation: 34972

You don't have to use {{ for variables inside {%. Just write:

<a href="{% url user.0 %}">{{user.0}}</a>

Upvotes: 2

Related Questions