Is it correct use for Django ModelForm - django

My question; i'm used ModelForm instead of forms.Form and i want to organize form in html file instead of forms.py file (i don't want to use attr tag because my form.html file is very complicated) Is this usage correct?
forms.py
class CommentForm(forms.ModelForm):
class Meta:
model=Comment
fields=[
'name',
'email',
'comment',
]
html file
<form class="post-comment-form" method="POST">
{% csrf_token %}
{% if form.errors %}
{% for error in form.non_field_errors %}
{{ error }}
{% endfor %}
{% endif %}
<div class="form-row">
<div class="form-item half blue">
<label for="id_name" class="rl-label" >Name</label>
{{ form.name}}
</div>
<div class="form-item half blue">
<label for="id_email" class="rl-label" >Email</label>
{{ form.email}}
</div>
</div>
<div class="form-row">
<div class="form-item blue">
<label for="id_comment" class="rl-label">Comment</label>
{{ form.comment }}
</div>
</div>
<button type="submit" class="save-button">Submit</button>

If you don't want to use the {{form}} then you have to give the input field for each model's fields like <input type="text" name = "name"> .For example
<div class="form-row">
<div class="form-item half blue">
<label for="id_name" class="rl-label" >Name</label>
**<input type="text" name = "name">**
</div>
<div class="form-item half blue">
<label for="id_email" class="rl-label" >Email</label>
<input type="text" name = "email">
</div>
</div>
<div class="form-row">
<div class="form-item blue">
<label for="id_comment" class="rl-label">Comment</label>
<input type="text" name = "comment">
</div>
</div>
<button type="submit" class="save-button">Submit</button>

On the html you can have something like this
{% csrf_token %}
{{ form.as_p }}
and then proceed with styling or better yet you can use {% crispy %} which personal I find it great based on what you want to archive, your forms.py looks ok.

Related

Problems with a backend part of search line in Django

who can explain me why my SearchView doesn't work. I have some code like this.It doesn't show me any mistakes, but it doesn't work. The page is clear. Seems like it doesn't see the input.
search.html
<div class="justify-content-center mb-3">
<div class="row">
<div class="col-md-8 offset-2">
<form action="{% url 'search' %}" method="get">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search..." />
<div class="input-group-append">
<button class="btn btn-dark" type="submit" id="button-addon2">Search</button>
</div>
</div>
</form>
</div>
</div>
</div>
search/urls.py
path('search/', SearchView.as_view(), name='search')
search/views.py
class SearchView(ListView):
model = Question
template_name = 'forum/question_list.html'
def get_queryset(self):
query = self.request.GET.get("q")
object_list = Question.objects.filter(
Q(title__icontains=query) | Q(detail__icontains=query)
)
return object_list
forum/question_list.html
{% extends 'main/base.html' %}
{% block content %}
{% for question in object_list %}
<div class="card mb-3">
<div class="card-body">
<h4 class="card-title">{{ question.title }}</h4>
<p class="card-text">{{ question.detail }}</p>
<p>
{{ question.user.username }}
5 answers
10 comments
</p>
</div>
</div>
{% endfor %}
{% endblock %}

Django - How to apply onlivechange on CharField / IntegerField

I want to hide the field form.name_of_parent_if_minor if the age (CharField) < 18 else show. I am not getting where to write jQuery or JS code or add separate js file. For this do I need to the html definition and add tag for age (CharField) or we can perform action on this as well.
I am new to the Django so if you find any mistakes in my code then any help would be appreciated for guidance.
forms.py
class StudentDetailsForm(forms.ModelForm):
class Meta:
model = StudentDetails
views.py
class StudentCreateView(LoginRequiredMixin, CreateView):
template_name = 'student/student_details_form.html'
model = StudentDetails
form_class = StudentDetailsForm
success_url = "/"
html
{% extends 'student/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div id="idParentAccordian" class="content-section">
<form method="POST">
{% csrf_token %}
<div class="card mt-3">
<div class="card-header">
<a class="collapsed card-link" style="display: block;" data-toggle="collapse"
href="#idPersonalInformation">Personal Information</a>
</div>
<div id="idPersonalInformation" class="collapse show" data-parent="#idParentAccordian">
<div class="card-body">
<div class="row">
<div class="col-6">
{{ form.joining_date|as_crispy_field }}
</div>
<div class="col-6">
{{ form.class|as_crispy_field }}
</div>
</div>
{{ form.student_name|as_crispy_field }}
{{ form.father_name|as_crispy_field }}
{{ form.mother_name|as_crispy_field }}
{{ form.gender|as_crispy_field }}
<div class="row">
<div class="col-6">
{{ form.date_of_birth|as_crispy_field }}
</div>
<div class="col-6">
{{ form.age|as_crispy_field }}
</div>
</div>
<div>
{{ form.name_of_parent_if_minor|as_crispy_field }}
</div>
</div>
</div>
</div>
<div class="form-group mt-3">
<button class="btn btn-outline-info" type="submit">Save</button>
<a class="btn btn-outline-secondary" href="javascript:history.go(-1)">Cancel</a>
</div>
</form>
</div>
{% endblock content %}
the easiest way but not the cleanest is to add JS script in your html
the recommanded way is to add static folder to your app and load static files in your html template using {% load static %}

Sort by ascending and descending using django-filter

I have the following code for few filterings:
from .models import ProductLaptop
import django_filters
class ProductLaptopFilter(django_filters.FilterSet):
laptops_name = django_filters.CharFilter(lookup_expr='icontains')
laptops_price = django_filters.NumberFilter()
laptops_price__gt = django_filters.NumberFilter(field_name='laptops_price', lookup_expr='gt')
laptops_price__lt = django_filters.NumberFilter(field_name='laptops_price', lookup_expr='lt')
class Meta:
model = ProductLaptop
fields = ['laptops_name', 'laptops_price', 'brand_name']
The html codes for this:
{% load widget_tweaks %}
{% block content %}
<form method="get">
<div class="well">
<h4 style="margin-top: 0">Filter</h4>
<div class="row">
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.laptops_name.label_tag }}
{% render_field filter.form.laptops_name class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.laptops_price.label_tag }}
{% render_field filter.form.laptops_price class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.brand_name.label_tag }}
{% render_field filter.form.brand_name class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.laptops_price__gt.label_tag }}
{% render_field filter.form.laptops_price__gt class="form-control" %}
</div>
<div class="form-group col-sm-4 col-md-3">
{{ filter.form.laptops_price__lt.label_tag }}
{% render_field filter.form.laptops_price__lt class="form-control" %}
</div>
</div>
<button type="submit" class="btn btn-primary">
<span class="glyphicon glyphicon-search"></span> Search
</button>
</div>
</form>
Which gives me a view like below:
Here I want to add an option where people can sort the items in ascending and descending order.
Can anyone give me some suggestions how can I implement this?
For that, you can use OrderingFilter from django-filter. Create this filter in your FilterSet class and provide all fields that should be enabled for ordering.

Django autopopulate entire form with model data based of single choice field

I am trying to make a modelform that populates all fields based on a single select field. Basically when the user selects a value from a dropdown it would then populate the rest of the fields based on data from a database.
models.py:
class Commands(models.Model):
command_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
command_prefix = models.TextField()
command = models.TextField()
args = models.TextField()
shell = models.TextField()
role = models.ForeignKey('Roles', models.DO_NOTHING)
os = models.ForeignKey('Operatingsystems', models.DO_NOTHING)
job_type = models.ForeignKey('Jobtypes', models.DO_NOTHING)
active = models.IntegerField()
views.py:
#verified_email_required
def jobs(request):
return render(request, 'backend/jobs.html', {'form': CommandsForm()})
forms.py:
class CommandsForm(Form):
name = ModelChoiceField(queryset=Commands.objects.filter(active=1).values('name'))
os = CharField(required=True, disabled=True)
command_prefix = CharField(required=True, disabled=True)
target = CharField(required=True)
command = CharField(required=True, disabled=True)
args = CharField(required=True, disabled=True)
shell = CharField(required=True, disabled=True)
urls.py
urlpatterns = [
url(r'^$', profile, name='profile'),
url(r'^jobs/$', jobs, name='jobs'),
url(r'^patchreport/$', patchreport, name='patchreport'),
url(r'^prtbl/$', PatchReportTable.as_view(), name='patchreptbl')
]
jobs.html
{% extends "backend/base.html" %}
{% load staticfiles %}
{% load widget_tweaks %}
{% block title %}
{{block.super}}Jobs
{% endblock %}
{% block content %}
<form id="jobs_form" class="form-horizontal text-center" method="post" action="{% url 'jobs' %}">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="form-group">
{{ form.name.errors }}
<label for="{{ form.name.id_for_label }}" class="col-sm-2 control-label">Name</label>
<div class="col-sm-8">
{{ form.name|add_class:"form-control" }}
</div>
</div>
<div class="form-group">
<label for="{{ form.os.id_for_label }}" class="col-sm-2 control-label">Os</label>
<div class="col-sm-8">
{{ form.os|add_class:"form-control" }}
</div>
</div>
<div class="form-group">
<label for="{{ form.command_prefix.id_for_label }}" class="col-sm-2 control-label">Command prefix</label>
<div class="col-sm-8">
{{ form.command_prefix|add_class:"form-control" }}
</div>
</div>
<div class="form-group">
<label for="{{ form.target.id_for_label }}" class="col-sm-2 control-label">Target</label>
<div class="col-sm-8">
{{ form.target|add_class:"form-control" }}
</div>
</div>
<div class="form-group">
<label for="{{ form.command.id_for_label }}" class="col-sm-2 control-label">Command</label>
<div class="col-sm-8">
{{ form.command|add_class:"form-control" }}
</div>
</div>
<div class="form-group">
<label for="{{ form.args.id_for_label }}" class="col-sm-2 control-label">Args</label>
<div class="col-sm-8">
{{ form.args|add_class:"form-control" }}
</div>
</div>
<div class="form-group">
<label for="{{ form.shell.id_for_label }}" class="col-sm-2 control-label">Shell</label>
<div class="col-sm-8">
{{ form.shell|add_class:"form-control" }}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button type="submit" class="btn btn-default" name="submit">Submit</button>
</div>
</div>
</form>
<script type="text/javascript" src="{% static "js/jobs.js" %}"></script>
{% endblock %}
To be honest, I am unsure of how to accomplish this with Django's ModelForms. Currently I took Michael Platt's advice and am auto-populating the fields with javascript on change event in a javascript file called jobs.js. I have to believe there is a way to accomplish the same thing by populating the entire form from the database directly, or via something like a RESTful api generated by TastyPie with an ajax call.
If you aren't too turned off by the concept of using javascript, you could use a .change() event for your particular select field. So it would look something like this:
$(document).ready(function(){
$("#id_name").change(function() {
// Find your different fields you want to populate and set the values here.
// Example would be
if ($("#id_name").val() == "Some value") {
$("#id_command_prefix").val("Whatever you want to populate with.")
}
});
});

Additional fields to model with separate forms

I have a model Customer which has some general fields like name phone etc and some fields that are more private like ssn id tax_id etc. so it is like this
class Customer(models.Model):
#general fields
#more private staff fields
I want to have one form to create a new customer but I want them to be in different tabs (well my employer does want that). What would be the best practice? Have different models so different forms?
class Customer(models.Models):
#general fields
class PrivateFields(models.Model):
#private fields
customer = models.OneToOneField(Customer)
The above method would require the usage of two different forms to be rendered in one template. If I want one form (first method one model) would something like that stand or will it have issues:
I am using bootstrap3:
<ul class="nav nav-tabs" id="myTab">
<li>General</li>
<li>Private</li>
</ul>
<form class="inline">
<div class="tab-content">
<div class="tab-pane active" id="general-info">
<div class="row">
<div class="form-group col-md-6">
<label for="text">Home</label>
<input type="text" id="text" class="form-control input-sm" placeholder="Small">
</div>
</div>
</div>
<div class="tab-pane" id="private-details">
<div class="row">
<div class="form-group col-md-6">
<label for="profile">Profile</label>
<input type="text" id="profile" class="form-control input-sm" placeholder="profile">
</div>
</div>
</div>
</div>
<div class="row">
<input type="submit" class="btn btn-primary" value="Submit form">
</div>
</form>
bootply link
I can't see any reason to have them in separate models, or even two separate forms. Tabs are just divs that are shown/hidden by some Javascript, so you can simply put the 'public' fields in one div and the 'private' ones in the other.
<div class="tab-pane" id="general-info">
<div class="field">
{{ form.generalfield1.label_tag }}
{{ form.generalfield1 }}
{{ form.generalfield1.errors }}
</div>
... etc ...
</div>
<div class="tab-pane" id="general-info">
<div class="field">
{{ form.privatefield1.label_tag }}
{{ form.privatefield1 }}
{{ form.privatefield1.errors }}
</div>
... etc ...
</div>
If you want to do it in a more generic way, perhaps you could define a list of public/private field names in the view:
public_fields = ['name', 'address']
private_fields = ['trustworthy', 'bank_details']
and check membership as you iterate through:
<div class="tab-pane" id="general-info">
{% for field in form %}
{% if field.name in public_fields %}
<div class="field">
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
</div>
{% endif %}
{% endfor %}
</div>
etc.