Licia Licia - 2 years ago 118
HTML Question

Django - Get data from a form that is not a django.form

I am working with Django in order to do a quite simple web application.

I am currently trying to have a succession of forms so that the user completes the information bits by bits.
I first tried to do it only in HTML because I wanted to really have the hand on the presentation. Here is my code.

I have two templates, create_site.html and create_cpe.html. I need to get informations from the first page in order to know what to ask in the second page.

Here is my create_site.html


<form action="{% url 'confWeb:cpe_creation' %}" method="post" class="form-creation">
{% csrf_token %}
<div class = "form-group row">
<label for="site_name" class="col-xs-3 col-form-label">Name of theSite</label>
<div class="col-xs-9">
<input id="site_name" class="form-control" placeholder="Name" required="" autofocus="" type="text">
<button class="btn btn-lg btn-primary btn-block" type="submit">Créer</button>

And here is the that I'm using to do all of this :

def site_creation(request):

template = loader.get_template('confWeb/create_site.html')
return HttpResponse(template.render(request))

def cpe_creation(request):
if request.method == "POST" :

What I would like to do is to get the value of the input of my form, inside my view "cpe_creation". I tried getting informations from the "request" object but I didn't manage to do that. Maybe I'm lacking very basic HTML skills but I thought the form information would be in the request body, but it wasn't (or didn't look like it).

I also tried using Django forms, but the problem is, I can't control very precisely what the form will look like. If I create an input, I can't tell it the size it's supposed to take or anything like that.

Hence, my questions are the following :
- How can I get the data from the submitted form ?
- Do I have to do it with Django's Forms ? If I do, how can I control the css class of an input ?

Answer Source

Your field in your form in your template has to have name attribute in order to pass it to your request.

Then you will be able to get it in your view like this:

site_name = request.POST.get('site_name')

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download