【问题标题】:Django does not recieve TextArea content in POST, but all other fields are sent fineDjango 没有在 POST 中收到 TextArea 内容,但所有其他字段都可以正常发送
【发布时间】:2017-05-29 19:16:05
【问题描述】:

我有一个 HTML 表单,我使用 Django 作为后端。所有其他字段都可以正常发送和接收,但application 不是。

apply.html

        <form action="/submit/" method="post">
            {% csrf_token %}

            <div class="row">
                <div class="three columns">
                    <label for="{{ form.username.id_for_label }}">Username</label>
                    <input name="{{ form.username.html_name }}" class="u-full-width" type="text"
                           placeholder="inventor02" id="{{ form.username.auto_id }}" maxlength="34" required>
                </div>
                <div class="three columns">
                    <label for="{{ form.discriminator.id_for_label }}">Discriminator</label>
                    <input name="{{ form.discriminator.html_name }}" class="u-full-width" type="number"
                           placeholder="4201" id="{{ form.discriminator.auto_id }}" maxlength="4" required>
                </div>
                <div class="six columns">
                    <label for="{{ form.current_rank.id_for_label }}">Current Rank</label>
                    <input name="{{ form.current_rank.html_name }}" class="u-full-width" type="text"
                           placeholder="Member" id="{{ form.current_rank.auto_id }}" maxlength="30" required>
                </div>
            </div>

            <label for="{{ form.application.id_for_label }}">Application</label>
            <textarea name="{{ form.application.html_name }}" class="u-full-width"
                      placeholder="I would like to be staff because..." id="{{ form.application.auto_id }}"
                      required></textarea>

            <input class="button button-primary" type="submit" value="Submit">
        </form>

views.py

def submit(request):
    if request.method == "POST":
        form = ApplicationForm(request.POST)
        print(request.POST)

        if form.is_valid():
            print(form)
            application = Application(username=form.cleaned_data["username"],
                                      discriminator=form.cleaned_data["discriminator"],
                                      current_rank=form.cleaned_data["current_rank"],
                                      application=form.cleaned_data["application"])
            application.save()

            return HttpResponse("<h1>Success!</h1>")
        else:
            return HttpResponse("Invalid form request. Try again.")
    else:
        return HttpResponse("You're accessing this using the wrong method. Go to the <a href=\"/apply\">apply</a> page.")

forms.py

class ApplicationForm(forms.Form):
    username = forms.CharField(max_length=34)
    discriminator = forms.IntegerField(max_value=9999)
    current_rank = forms.CharField(max_length=30)
    application = forms.TextInput()

models.py

class Application(models.Model):
    username = models.CharField(max_length=34)
    discriminator = models.PositiveIntegerField()
    current_rank = models.CharField(max_length=50, default="Member")
    application = models.TextField()
    status = models.NullBooleanField(default=None)
    status_reason = models.TextField(default="Not yet reviewed")

    def __str__(self):
        return self.username + "#" + str(self.discriminator)

【问题讨论】:

    标签: python html django forms post


    【解决方案1】:

    你真的应该在这里使用ModelForm。但无论如何,我认为您的问题与糟糕的Form 定义有关:

    class ApplicationForm(forms.Form):
        username = forms.CharField(max_length=34)
        discriminator = forms.IntegerField(max_value=9999)
        current_rank = forms.CharField(max_length=30)
        # application = forms.TextInput()
        application = forms.CharField(widget=forms.Textarea)
    

    这应该可以解决问题。你真的应该尝试转移到ModelForm,因为它会使保存模型等操作变得更加容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 2014-11-10
      • 1970-01-01
      • 2011-10-19
      • 1970-01-01
      相关资源
      最近更新 更多