[Fixed]-How to manipulate user submitted text and display it with django?

1๐Ÿ‘

โœ…

  1. Define a form; in forms.py under your appโ€™s folder

    class MyForm(forms.Form):
        myinput = forms.forms.CharField(max_length=100)
    
  2. Define a function in your views.py

    import .forms
    def handle_form(request):
        if request.method == 'POST': # If the form has been submitted...
            form = MyForm(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                # Process the data in form.cleaned_data
                # ...
                return HttpResponseRedirect('/thanks/') # Redirect after POST
        else:
            form = MyForm() # An unbound form
    
        return render(request, 'handle_form.html', {
            'form': form,
        })
    
  3. Add a template

        <form action="" method="post">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="Submit" />
        </form>
    

Of course you need to add it to your urls.py
Most info was copy pasted from: https://docs.djangoproject.com/en/1.8/topics/forms/

๐Ÿ‘คacidjunk

Leave a comment