首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在django中刷新页面时出错

在django中刷新页面时出错
EN

Stack Overflow用户
提问于 2017-01-18 14:41:50
回答 1查看 90关注 0票数 0

我正在学习使用django创建一个网站。我有一个主页,通过它用户可以选择城市。下一页显示了hotels.It的列表正常工作,但是当页面刷新时,它会给出错误

这是我的列表页面的html文件。

代码语言:javascript
复制
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
 <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
 <script src="//code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>
<script  type="text/javascript" src="../js/typeahead/0.11.1/typeahead.bundle.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>

{% load static %}
<script src="/static/typeahead.js "></script>



</head>
<body>

<div >
<div style="float:left; background-color:#d9d9d9;margin-top:100px;width:30%;height:800px;">
<h2 style="text-align:center;">Filters</h2>
<div  style="width:95%; margin-left: 10px; " >
    <form method="post" data-ajax="false" action="{% url 'list' %}">
     {% csrf_token %}

       <div data-role="rangeslider" data-mini="true" style="width:100%">
    <label for="range-1a">price:</label>
    <input name="range-1a" id="range-1a" min="0" max="100" value="0" type="range">
    <label for="range-1b">Rangeslider:</label>
    <input name="range-1b" id="range-1b" min="0" max="100" value="100" type="range">
  </div>
  <input type="submit"  value="Submit">
      </form>
  </div>
</div>
<div style=" float:right;margin-right:30px;margin-top:100px;width:60%;height:80%">



{% for hotel in city_list %}
{% load static %}

   <div class="col-md-4" style="width:45%;border:0;position:relative;">
    <div class="thumbnail" style="height:300px; background-color:black;border:0;border-radius:0;position:relative;box-shadow:0;" >
      <a href="/w3images/lights.jpg" data-ajax="false">
        <img src="{% static hotel.photo.url %}" style="border:0;height:85%;width:100%;"  >
        <div class="caption">
          <p style="color:white;">Name</p>
        </div>
      </a>
    </div>
  </div>






{% endfor %}

</div>  


</div>



</body>
</html>

这是我的views.py

代码语言:javascript
复制
def homepage(request):
    hotel_list=Hotels.objects.all()
    context={'hotel_list':hotel_list}
    return render(request, 'polls/homepage.html',context)

def wholelist(request):
    hotelvar=request.POST.get('service_type')
    if hotelvar=='Hotels':

      city_list=Hotels.objects.filter(city_name__iexact=request.POST.get('searchabc'))
    if not city_list:
        hotel_list=Hotels.objects.all()
        context={'hotel_list':hotel_list}
        return render(request, 'polls/homepage.html',context)

    pricemin=200
    pricemax=800

    context={'pmin':pricemin,'pmax':pricemax,'city_list':city_list}
    return render(request, 'polls/list.html',context)

在赋值之前引用的/polls/wholelist/ local variable city_list处刷新UnboundLocalError时,我得到以下错误

city_list正在从主页传递,但当页面重新加载时,它不会传递。有没有办法在页面刷新时传递它?

EN

回答 1

Stack Overflow用户

发布于 2017-01-18 14:58:34

hotelvar不等于'Hotels‘时,不设置变量city_list

代码语言:javascript
复制
def wholelist(request):
    hotelvar=request.POST.get('service_type')
    city_list = None
    if hotelvar=='Hotels':
          city_list=Hotels.objects.filter(city_name__iexact=request.POST.get('searchabc'))
    if not city_list:
        hotel_list=Hotels.objects.all()
        context={'hotel_list':hotel_list}
        return render(request, 'polls/homepage.html',context)

    pricemin=200
    pricemax=800
    context={'pmin':pricemin,'pmax':pricemax,'city_list':city_list}
    return render(request, 'polls/list.html',context)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41712928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档