Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<body>

<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">
        </div>
    </div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Créer</button>
</form>
</body>

And here is the views.py 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" :
        print(request.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 ?

like image 802
Licia Avatar asked Sep 16 '16 13:09

Licia


People also ask

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What is form Is_valid ()?

Form. is_valid () The primary task of a Form object is to validate data. With a bound Form instance, call the is_valid() method to run validation and return a boolean designating whether the data was valid: >>> data = {'subject': 'hello', ... '

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is form Is_bound?

is_bound attribute and is_valid() method We can use is_bound attribute to know whether the form is inbound state or not. If the form is in the bound state then the is_bound returns True , otherwise False . Similarly, we can use the is_valid() method to check whether the entered data is valid or not.


2 Answers

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')

like image 199
sebb Avatar answered Sep 28 '22 06:09

sebb


Your form field needs a name attribute.

            <input id="site_name" name="site_name" class="form-control" placeholder="Name" required="" autofocus="" type="text">

Alternatively, if you want to use Django forms, you can set the CSS class of a form field by explicitly defining the widget.

class SiteForm(forms.Form):
    site_name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}))

Judging from the class form-control, I assume you want to use Bootstrap. Check out django-crispy-forms, which makes it really easy style Django forms with Bootstrap.

like image 20
Daniel Hepper Avatar answered Sep 28 '22 06:09

Daniel Hepper