【问题标题】:Django 1.6: Form not displaying in templateDjango 1.6:模板中未显示表单
【发布时间】:2014-08-12 01:10:13
【问题描述】:

我正在尝试实现一个可以获取用户输入的表单,但由于某种原因,该表单没有显示在模板中。我在表单中有两个字段,其中一个字段是下拉菜单。模板未显示下拉列表。

这是我尝试使用的表单

TIME_CHOICES = (
    (5, 'Less than 5 Minutes'),
    (10, 'Less than 10 Minutes'),
    (15, 'Less than 15 Minutes'),
)  
class UserContentForm(forms.ModelForm):
    time = forms.ChoiceField(required=True, choices = TIME_CHOICES, widget = forms.Select)
    comment = forms.CharField(max_length=2000, required= False,widget=forms.TextInput())

    class Meta:
        model = UserContent
        fields = ("time","comment")

这是我想要保存表单的视图

def addContent(request, id):
    d = getVariables(request)
    profile = Doctor.objects.get(id=id)

    if request.user.is_authenticated():
        user = request.user
        ds = DoctorSeeker.objects.get(user=user)
        d['doctorseeker'] = ds

        doctorLiked = Like.objects.filter(doctor_id=profile.id,user_id=user.id)

        d['my_doctor'] = profile.id == request.user.id

        d['username'] = user.username

        if doctorLiked:
            d['liked'] = True
        else:
            d['liked'] = False


    if request.method == "POST":
        form = UserContentForm(request.POST)

        if form.is_valid():
            time = form.cleaned_data['time']
            comment = form.cleaned_data['comment']
            con = UserContent(time=time, comment = comment, doctor_id = profile.id, user_id = request.user.id)
            con.save()

            return render(request,'meddy1/docprofile.html',{'doctor': profile})

    else:
        form = UserContentForm()

    d.update({'doctor': profile, 'UGC': UserContent.objects.all()})
    return render(request, 'meddy1/usercontent.html',d)

这是我要渲染的模板

<form action="" method="post" id="user_uploader" > {% csrf_token %}

      <input type="hidden" name="user" value="{{ user.id }}" />
      <input type="hidden" name="doctor" value="{{ doctor.id }}" />

      <select class="form-control" id="s1" name="time">
          <option><b>Select a Time...</b></option>
          {% for value, text in form.time.field.choices %}
            <option value="{{ value }}">{{ text }}</option>
          {% endfor %}
        </select>

      <input type="text" class="form-control" id="comment" placeholder="Comment" name="comment">



      <button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Submit Review</button>

    </form>

这是模型

class UserContent(models.Model):
    time = models.IntegerField(blank = True)
    comment = models.TextField(blank = True)
    doctor = models.ForeignKey(Doctor)
    user = models.ForeignKey(User)
    submitted_on = models.DateTimeField(auto_now_add=True)

【问题讨论】:

    标签: django forms templates


    【解决方案1】:

    您没有将form 变量传递给模板。将行更新为

    d.update({'doctor': profile, 'UGC': UserContent.objects.all(),
              'form': form #add form variable
              })
    

    另外,您可以使用{{ form.time }} 来渲染它,而不是手动渲染选择标签。

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 2017-10-20
      • 2018-08-21
      • 1970-01-01
      • 2017-12-15
      • 2021-04-01
      • 2023-01-10
      • 2014-01-29
      相关资源
      最近更新 更多