2017-04-20 72 views
-2

我正在創建用戶創建並且贊成的所有帖子的摘要頁面。但是,它試圖檢索用戶上傳的帖子時拋出上面的錯誤,我不知道爲什麼?name'user'沒有被定義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 %} 
+2

而不是'user = user',你想'user = request.user'。 – castis

回答

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

第一行不是必需的,request.user已經是一個User對象。 – user6731765