在Django中编辑模板时如何显示选中的项目和上载的图像值

时间:2019-04-21 06:50:45

标签: django django-templates

我有一个简单的豆腐应用程序。这是编辑/更新,一切正常。唯一的事情是我要更新时,我看不到已经检查过的课程,已经添加的日期和已经上传的图像。在edit_student页面

中检索课程,日期和图像
  

models.py

class Student(models.Model):
name = models.CharField(max_length=100)
courses = models.CharField(max_length=250)
address = models.CharField(max_length=200)
email = models.EmailField()
phone = models.CharField(max_length=15)
image = models.ImageField(upload_to='Students',blank=True)
joined_date = models.DateField()

def __str__(self):
    return self.name
  

views.py

def editstudent(request,id):
    student = Student.objects.get(id=id)
    courses = Course.objects.all()
    return render(request,'students/edit_student.html',{'student':student,'courses':courses})
def updatestudent(request,id):
    student = Student.objects.get(id=id)
    form = AddStudentForm(request.POST,instance=student)
    if form.is_valid():
        student = form.save(commit=False)
        student.save()
        messages.success(request,'Student with name  {}  updated'.format(student.name))
        return redirect('students:view_student')
    else:
        messages.error(request,'Error in form.Try Again')
    return render(request,'students/edit_student.html',{'student':student})
  

edit_student.html

<form action="{% url 'students:update_student' student.id %}" method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    <div class="form-group">
                        <h5>Full Name <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="text" name="name" class="form-control" value="{{student.name}}" required data-validation-required-message="This field is required"> </div>
                    </div>
                 <div class="form-group">
                   <h5>Courses <span class="text-danger">*</span></h5>
                   <div class="controls">
                     {% for course in courses %}
                     <input name ="courses" type="checkbox" id="course-{{course.id}}" value="{{course.title}}">
                     <label for="course-{{course.id}}">{{course.title}}</label>
                     {% endfor %}
                   </div>
                 </div>
                    <div class="form-group">
                        <h5>Address<span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="text" name="address" class="form-control" value="{{student.address}}" required data-validation-required-message="This field is required"> </div>
                    </div>
                    <div class="form-group">
                        <h5>Phone Number <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="text" name="phone" value="{{student.phone}}" data-validation-required-message="This field is required" class="form-control" required> </div>
                    </div>
                    <div class="form-group">
                        <h5>Email <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="email" name="email"  value="{{student.email}}" data-validation-required-message="This field is required" class="form-control" required> </div>
                    </div>
                      <div class="form-group">
                        <h5>Joined Date <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="date" name="joined_date" value="student.joined_date}}" data-validation-required-message="This field is required" class="form-control" required> </div>
                    </div>
                    <div class="form-group">
                        <h5>Image <span class="text-danger">*</span></h5>
                        <div class="controls">
                            <input type="file" name="image" value="/media/{{student.image}}" class="form-control" > </div>
                    </div>
                    <div class="text-xs-right">
                        <button type="submit" class="btn btn-info">Submit</button>
                    </div>
                </form>

0 个答案:

没有答案