是Django的新手。基本上我有所有城市名称的页面,我使用管理页面和模型添加了它们。现在,如果我单击任何城市名称,它应该路由到该城市的Wiki页面。我的问题是如何获取我单击的城市名称,以及如何动态地在URLS中传递这些城市名称,并在该城市名称旁边附加一个城市名称,然后调用各个维基页面。
这是我的城市HTML片段,其中dests.name是我的城市名称
{%for dests in dests%}
<!-- Destination -->
<div class="destination item">
<div class="destination_image">
<img src="{{dests.image.url}}" alt="">
{% if dests.offer %}
<div class="spec_offer text-center"><a href="#">Special Offer</a></div>
{% endif %}
</div>
<div class="destination_content">
<div class="destination_title"><a href="destinations">{{dests.name}}</a></div>
<div class="destination_subtitle">
<p>{{dests.desc}}</p>
</div>
<div class="destination_price">From ${{dests.price}}</div>
</div>
</div>
{%endfor%}
我当前的视图具有用于单击城市名称时检查用户身份验证和加载示例html的代码
class Destinations:
def destinations(request):
# Login.login(request)
if request.user.is_authenticated:
return render(request,'sample.html')
else:
print("In Destinations else loop")
return redirect('login')
您的HTML模板将是这样的:
{%for dest in dests%}
<!-- Destination -->
<div class="destination item">
<div class="destination_image">
<img src="{{dest.image.url}}" alt="">
{% if dest.offer %}
<div class="spec_offer text-center"><a
href="#">Special Offer</a></div>
{% endif %}
</div>
<div class="destination_content">
<div class="destination_title"><a
href="{% url 'destination_detail' dest.name %}">{{dest.name}}</a></div>
<div class="destination_subtitle">
<p>{{dest.desc}}</p>
</div>
<div class="destination_price">From
${{dest.price}}</div>
</div>
</div>
{%endfor%}
并为您的详细信息视图创建如下的URL。
path('destination/<str:city>', destination, name='destination_detail'),
并且在您的查看功能destination
中,您可以从]获取通过url发送的城市>
kwargs['city']
您可以将查看方法更改为以下内容:
def destinations(request, *args, **kwargs): destination_city = kwargs[‘city’]
然后您可以在所需的任何位置使用此
destination_city
,它将具有来自URL的值。
我的html