如何使用FBV初始化Django形式的数据(变量)?

时间:2019-07-19 14:50:57

标签: django django-rest-framework django-forms django-templates django-views

我有一个联系表,用户可以在其中填写其信息以发送电子邮件/反馈

Forms.py

subjects = (
    (1, 'Bug/error reporting'),
    (2, 'Enhancement suggestion'),
    (3, 'Support and help')
    (4, 'Geral feedback')
)

class ContactForm(forms.Form):

    name =      forms.CharField(initial=)
    email =     forms.CharField(initial=)
    subject =   forms.CharField(max_length=720,label="Subject",required=True)
    message =   forms.ChoiceField(required=True,choices=subjects)

我如何向每个当前用户提供此表单的初始数据及其数据(电子邮件/姓名)

views.py

def emailView(request):
    user = request.user
    initial_data = {
        'name' :    user.FirstName,
        'email' :   user.email
    }
    if request.method == 'GET':
        form = ContactForm(initial=initial_data)
    else:
        form = ContactForm(request.POST,initial=initial_data)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            from_email = user.email
            message = form.cleaned_data['message']
            try:
                send_mail(subject, message, from_email, ['admin@example.com'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('success')
        return render(request, "email.html", {'form': form})

模板

          <div class="modal fade" id="modalContactForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
          aria-hidden="true">

        <form method="POST" action="{% url 'contact' %}">
                {% csrf_token %}

      <div class="modal-dialog" role="document">
        <div class="modal-content">

      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Write to us</h4>

        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">

           <div class="md-form">
            <label data-error="wrong" data-success="right" for="form8">your Name</label>
            <textInput type="text" id="form8" class="md-textarea form-control" rows="4" name="name"></textInput>
        </div>
           <div class="md-form">
            <label data-error="wrong" data-success="right" for="form8">email</label>
            <textinput type="text" id="form8" class="md-textarea form-control" rows="4" name="email"></textinput>
        </div>
        <div class="md-form">
          <label data-error="wrong" data-success="right" for="form8">subject</label>
            <select name="subject" class="form-control">
                <option value=1>Bug/error reporting</option>
                 <option value="2">Enhancement suggestion</option>
                 <option value="3">Support and help</option>
                <option value="4">Geral feedback</option>
            </select>
        </div>
        <div class="md-form">
            <label data-error="wrong" data-success="right" for="form8">Your message</label>
            <textarea type="text" id="form8" class="md-textarea form-control" rows="4" name="message"></textarea>
        </div>

      </div>
      <div class="modal-footer d-flex justify-content-center">
        <button class="btn btn-info">Send</button>
      </div>
    </div>
  </div>
        </form>
</div>

我试图在视图中使它像这样,但这是不可编辑的,并且没有填写在表单中。 名称和电子邮件不会显示为可点击字段,也不会显示数据。

0 个答案:

没有答案