在django模板中使用数组元素作为相对URL

时间:2018-03-11 11:40:32

标签: python html django

我对django来说是全新的,并且一直在努力解决以下问题。 我将一个排序的列表列表发送到一个模板,从views.py到模板,其中应显示每个子列表(包含两个元素)。我希望第一个元素是一个链接,以这种方式,String内容应该是要显示的相对URL。

所以我使用了以下语法:

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

但是它给了我以下错误:

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

请在下面找到相关的代码段:

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)

模板:

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

对于个人资料:

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)

应用程序/ 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')
]

项目/ 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)

感谢任何可以提供帮助的人!

2 个答案:

答案 0 :(得分:2)

您不必对{{内的变量使用{%。只需写下:

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

答案 1 :(得分:1)

应该是:

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

考虑到你:

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

其中:

  • url_namename
  • url
  • arg_1arg_1是您的arguments用于您的网址(如果有的话,如果不是没有它们的话)。

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

<强>侧面说明: 一旦您 {%here%} ,所有推送的上下文都可以使用而不需要任何额外的{{}}