inventor02
inventor02

Reputation: 58

Django does not recieve TextArea content in POST, but all other fields are sent fine

I have a form in HTML, and I am using Django as a backend. All the other fields are sent and recieved fine, but application is not.

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)

Upvotes: 0

Views: 2178

Answers (1)

2ps
2ps

Reputation: 15926

You really should be using a ModelForm here. But in any case, I think your issue has to do with a poor Form definition:

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)

And that should fix the problem. You should really try to move to a ModelForm as it will make doing things like saving your models much easier.

Upvotes: 1

Related Questions