Django没有收到POST中的TextArea内容,但所有其他字段都发送得很好

时间:2017-01-14 14:41:43

标签: python html django forms post

我有一个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)

1 个答案:

答案 0 :(得分:0)

你真的应该在这里使用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,因为这样可以更轻松地保存您的模型。