how to grab a data from existing field in Django - django

I have a web page that I have to connect with Django. So I have an existing form with input and submit button. I have created a model and form. I put {{form}} tag and a little unstyled form appeared. but I want to grab a data from the one in HTML file. The code:
form file:
class NumberForm(ModelForm):
class Meta:
model = Number
fields = "__all__"
HTML:
<form action="#" method="post">
{% csrf_token %}
{{ form }}
<div class="line>
<input type="text" class="form-control" name="phoneNumber" id="phoneNumber2" value="" placeholder="+7 (___) ___-__-__">
<button class="button_blue">t</button>
</div>
<label class="container_checkbox">
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
</form>

Related

Sending data in django without creating forms

There is already a subscribe field in my html page.
I don't want to create another django form.
how can I send data from here?
<div class="input-group">
<span class="input-group-addon fh5co_footer_text_box" id="basic-addon1"><i class="fa fa-envelope"></i></span>
<input type="text" class="form-control fh5co_footer_text_box" placeholder="Enter your email..." aria-describedby="basic-addon1">
<i class="fa fa-paper-plane-o"></i> Subscribe
</div>
You can put your code inside form tags that points to your django view:
<form action="url_of_your_view" method="post">
{% csrf_token %}
</form>
and you can get the data in your view using request.POST but don't forget the {% csrf_token %} in your form.

Error retrieving value textarea's value from form in Django

I am new to Django. I am trying to create a simple form that has text area in it. I can't seem to retrieve the value from textarea
Here's my form:
<form method="POST" class="ui form" action="">
{% csrf_token %}
<div class="field">
<label>Title</label>
<input type="text" name="title"/>
<label> Content</label>
<textarea rows="8" name="content" ></textarea>
</div>
<button type="submit" class="ui primary button">Create</button>
</form>
Here's my how I handle the form
def createArticle(request):
if(request.method == 'POST'):
form = ArticleForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
body = form.cleaned_data['content']
username = request.session['email']
user = User.objects.get(username=username)
I am getting this error KeyError at /create/article/
'content' at this line body = form.cleaned_data['content']
Here's the content from my post request
csrfmiddlewaretoken'CgCwDF1d03KGIxsmM2Z4hhStBRIxw9hlh1ACtYXpBWXLLvYJq2tfdO7lqds7EVxI'
title'dsadasdasdsadsadasdsadasdsadsad'
content'<p>sadasdsadasdasdasdsadasdasdsadsadsadasdsadsadsadsadsadsadsadasdasd</p>\r\n'
I am guessing the <p> tag in content is what causing the error but I have no idea idea how to get rid of that.
You'll need to define name and id attributes for the form, and also define a form attribute for the textarea whose value would be the value of the form's id attribute.
<form method="POST" class="ui form" action="" id="content" name="content">
{% csrf_token %}
<div class="field">
<label>Title</label>
<input type="text" name="title"/>
<label> Content</label>
<textarea rows="8" form="content" ></textarea>
</div>
<button type="submit" class="ui primary button">Create</button>
</form>

Rendering fields manually and custom css class

I am trying to manually render the form fields.
I would like to add a bootstrap and custom css class to rendered html.
How can I do this ?
forms.py
class OrderCreateForm(forms.ModelForm):
class Meta:
model = Order
fields = ['name','postal_code']
widgets = {
'postal_code': forms.RadioSelect(),
}
file.html
<form action="." method="post" class="order-form">
<div class="fieldWrapper">
{{ form.postal_code }}
</div>
<p><input type="submit" value="Send"></p>
{% csrf_token %}
</form>
{% endblock %}
rendered html
<div class="fieldWrapper">
<ul id="id_postal_code">
<li><label for="id_postal_code_0"><input type="radio" name="postal_code" value="Yes" required id="id_postal_code_0" />Yes</label></li>
<li><label for="id_postal_code_1"><input type="radio" name="postal_code" value="No" required id="id_postal_code_1" />No</label></li>
</ul>
</div>
How to solve a problem ?
I would appreciate your help.
You can add css classes to form fields in the following way:
'postal_code': forms.RadioSelect(attrs={"class": "form-control"}),
Another option is to not render the form this way at all, and just write your own html.

django-ckeditor request dont get changes in form

I have one form called in ajax function, this function return one django form with one ckeditor field.
This field is displayed without problems, but when I make a request, the field value, dont sended in request, but if I make another, in the same form, with the same values, the value is update and is sended in request.
My form field
class EditCommentForm(IdeiaForm):
content = forms.CharField(
max_length=settings.COMMENT_TEXT_LIMIT if hasattr(settings, "COMMENT_TEXT_LIMIT") else 10000,
required=True,
widget=CKEditorWidget(config_name='question')
)
comment_id = forms.IntegerField(required=True)
My html template
<form class="create-comment" data-group-class=".comment-group" data-ajaxform="true" data-toggle="replace" class="create-comment" data-update="#{{ to_update }}" action="{% url 'comment:edit' %}" method="post">{% csrf_token %}
<div class="comment-group create-comment-body{% if form.content.errors %} has-error{% endif %}">
<textarea name="content" class="form-control" placeholder="Deixe seu comentário">{{ instance.content }}</textarea>
<span class="help-block"></span>
</div>
<input name="comment_id" value="{{ instance.id }}" type="hidden"/>
<div class="create-comment-footer">
<input type="submit" value="Editar" class="btn btn-primary">
</div>
</form>

django model form with bootstrap doesn't work

I am making a website using django but I have a problem with my modelforms. This is my working form code in my html-file:
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
{% csrf_token %}
{{ form.as_p }}
</div>
<input type="submit" value="Inschrijven!" class="btn btn-primary" />
</form>
If i use this code, everything works fine, when I fill in the form and press send, I can see the information from my admin-page.
I now tried to style my form using bootstrap:
<form class="form-horizontal" role="form" method="post" action="">
{% csrf_token %}
{% for field in form %}
<div class="form-group">
<label class="col-sm-4 control-label">{{ field.label }}</label>
<div class="col-sm-4">
<input type="text" class="form-control" placeholder="{{field.label}} ingeven">
</div>
</div>
{% endfor %}
<input type="submit" value="Inschrijven!" class="btn btn-primary">
</form>
Now I have a nice bootstrap layout but if I fill in the form, I can't see the information at my admin-page.
Does somebody know how I can fix this so I can keep the nice layout and still have my form to work?
When you output Django HTML form, you will notice that it creates input elements with the following ID and Name attributes of the element.
Eg
class MyForm...
name = ...
age = ...
HTML
<input type='text' id='id_name' name='name' ...
<input type='text' id='id_age' name='age'...
To fix your issue, in your HTML add the following attribute to the HTML input field you're generating
id="id_{{field.name}}" name="{{field.name}}"
Reasoning:
When the form is submitted, the browser will send form encoded data in the form of name=value pair, which then arrives in form of dict in request.POST and when fed into Django form, it pre fills the form by matching the Django form attribute name to POST values.