'image'属性没有与之关联的文件

时间:2019-10-16 09:54:03

标签: python django

自从我向model.py添加了ImageField以来,我一直在尝试更新图像,但是它总是给我这个错误'image'属性没有与之关联的文件。

可以从Django提供的“管理”面板中进行添加和更新,但是我需要创建自己的更新图像的方式,但是id不起作用。

这是我的附件

这是我的模特

class rooms(models.Model):
    room_number = models.PositiveIntegerField(unique = True)
    room_type = models.ForeignKey(room_types, on_delete=models.CASCADE)
    number_of_beds = models.PositiveIntegerField()
    image = models.ImageField(upload_to = 'room_pics/', null = True, blank = True)
    price = models.PositiveIntegerField(default=0)
    class Meta:
        verbose_name_plural = 'Rooms'

        def __str__(self):
            room_number = "Room number: " + str(self.room_number)
            return room_number

这是来自forms.py的表格

class addRoomForm(forms.ModelForm):
    class Meta:
        model = rooms
        fields = ['room_number', 'room_type', 'number_of_beds', 'price', 'image']

这是来自views.py的视图,用于添加和更新

def add_room(request):
    form = addRoomForm()
    if request.method == 'POST':
        form = addRoomForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, f'Room added successfully!')
            return redirect('add_room')
        else:
            form = addRoomForm()
       context = {
           'form' : form,
       }
       myTemplate = 'hotel/addRoom.html'
       return render(request, myTemplate, context)

def update_room(request, id):
    instance = get_object_or_404(rooms, pk = id)
    form = addRoomForm(request.POST or None, instance = instance)
    if form.is_valid():
        form.save()
        messages.success(request, f'Room has been updated successifully!')
        return redirect ('view_rooms')
        context = {
            'form': form,
            }
         myTemplate = 'hotel/addRoom.html'
         return render(request, myTemplate, context)`

,模板源代码为

<div class=" w3-card">
    <table class="table table-hover table-bordered">
        <thead>
        {% if room.image.url %}
            <tr>
                <img src="{{room.image.url}}" alt="image" width="711px" height="400px;">
            </tr>
        {% endif %}
        </thead>
        <tbody>
            <tr>
                <td>Room number</td>
                <td>{{room.room_number}}</td>
            </tr>
            <tr>
                <td>Room Type</td>
                <td>{{room.room_type}}</td>
            </tr>
            <tr>
                <td>Number of beds</td>
                <td>{{room.number_of_beds}}</td>
            </tr>
            <tr>
                <td>Price</td>
                <td>{{room.price}}</td>
            </tr>
            <tr>
                <td><a href="{% url 'update_room' room.id %}" class="btn btn-primary"><i class="fa fa-edit"></i></a>
                   <a href="{% url 'delete_room' room.id %}" class="btn btn-danger" onclick="return confirm('Seriously delete room number {{room.room_number}} !?');"><i class="fa fa-trash"></i></a>

                 </td>
            </tr>
        </tbody>
    </table>
</div>

1 个答案:

答案 0 :(得分:0)

应为form = addRoomForm(request.POST, request.FILES)

请参见File Uploads,这是为您提供详细且有据可查的教程。