名称'user'未定义django

时间:2017-04-20 20:24:01

标签: django

我正在创建用户创建的所有帖子的摘要页面,也是受欢迎的。但是,它在尝试检索用户上传的帖子时会抛出上述错误而我不知道为什么?

模型

class Aircraft(AircraftModelBase):
    user = models.ForeignKey(User)
    manufacturer = SortableForeignKey(Manufacturer)
    aircraft_type = SortableForeignKey(AircraftType)
    body = SortableForeignKey(Body)
    engines = models.PositiveSmallIntegerField(default=1)

查看

def account_overview(request):

  fav_aircraft = FavoritedAircraft.objects.filter(user__id=request.user.id)

  uploaded_aircraft = Aircraft.objects.filter(user=user) <---- HERE!!!!

  fav_airline = FavoritedAirline.objects.filter(user__id=request.user.id)

  return render(request, 'account/account_overview.html', {'favAircraft':fav_aircraft,
                                                           'favAirline':fav_airline,
                                                           'UploadedAircraft':uploaded_aircraft})

模板

{% if UploadedAircraft %}
<div class="col-md-12">
    <i><h1><strong>Your Aircraft Uploads..</strong></h1></i>
    {% for aircraft in UploadedAircraft %}
    <div class="col-lg-offset-0 col-md-4 col-sm-3 item">
        <div class="box"><img src="{{ aircraft.aircraft.image.url }}" width="200px" height="200px" alt="{{ aircraft.aircraft.title }}"/></a>
            <h3 class="name">{{ aircraft.aircraft.name }}</h3>
            <h4><em>Range: {{ aircraft.aircraft.maximum_range }}</em></h4>
            <a href="{% url 'aircraft_update' %}"><button class="btn btn-default" type="button">Edit </button></a>
            <button class="btn btn-default" type="button">Delete </button>
        </div>
    {% endfor %}
    </div>
 </div>
{% else %}
      <h2 class="text-center">Opps.. You don't seem to have any uploads..</h2></div>
{% endif %}

1 个答案:

答案 0 :(得分:0)

#your user is object of User model. Aircraft has foreign key from User model
#Try this code  
uploaded_aircraft = Aircraft.objects.filter(user=user)