I have a number of Boolean Field checkbox inputs within a form and I have noticed the following Boolean Fields (avaialble, bfbw, asp and tours) are printing a default value of 'False' whilst the remainder of the Boolean Fields (para_athlete, working_with_children) do not.
When the value of False is there I cannot toggle the checkbox between on/off (True/False)
Is there something obvious that I am missing as I am struggling to see any difference between the fields?
Thanks in advance for any help I can get on this.
Below is the code.
Model
athlete_ref = models.CharField(max_length=10, default=1)
athlete_name = models.CharField(max_length=80)
email = models.EmailField()
phone_number = models.CharField(max_length=120)
home = models.CharField(max_length=120)
education = models.CharField(max_length=120)
sport = models.CharField(max_length=120, choices=sports, default='Shooting')
notes = models.TextField()
gender = models.CharField(max_length=120, choices=genders, default='Not Specified')
para_athlete = models.BooleanField(blank=True)
working_with_children = models.BooleanField(blank=True)
expiry_date = models.DateField(blank=True, null=True)
available = models.BooleanField(blank=True)
available_from = models.DateField(blank=True, null=True)
bfbw = models.BooleanField(blank=True)
latest_bfbw_session = models.DateField(blank=True, null=True)
number_bfbw_sessions = models.CharField(blank=True, null=True, max_length=10)
asp = models.BooleanField(blank=True)
latest_asp_session = models.DateField(blank=True, null=True)
number_asp_sessions = models.CharField(blank=True, null=True, max_length=10)
tours = models.BooleanField(blank=True)
latest_tours_session = models.DateField(blank=True, null=True)
number_tours_sessions = models.CharField(blank=True, null=True, max_length=10)
Form
class EditAthleteForm(forms.ModelForm):
class Meta():
model = Athlete
fields = ('__all__')
widgets = {
'athlete_ref': TextInput(attrs={'id':'athlete_ref', 'name': 'athlete_ref', 'hidden': 'hidden'}),
'athlete_name': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_name', 'name': 'edit_athlete_name', 'placeholder': 'John Doe'}),
'email': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_email', 'name': 'edit_athlete_email', 'placeholder': 'name#example.com'}),
'phone_number': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_phone_number', 'name': 'edit_athlete_phone_number', 'placeholder': '0400 000 000', 'pattern': '^((\+61\s?)?(\((0|02|03|04|07|08)\))?)?\s?\d{1,4}\s?\d{1,4}\s?\d{0,4}$'}),
'home': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_home', 'name': 'edit_athlete_home', 'placeholder': 'Home Address'}),
'education': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_education', 'name': 'edit_athlete_education', 'placeholder': 'Education'}),
'sport': Select(attrs={'class': 'form-select', 'id': 'edit_athlete_sports', 'name': 'edit_athlete_sports'}),
'notes': Textarea(attrs={'class': 'form-control', 'id': 'edit_athlete_notes', 'name': 'edit_athlete_notes'}),
'gender': Select(attrs={'class': 'form-select', 'id': 'edit_athlete_gender', 'name': 'edit_athlete_gender'}),
'para_athlete': CheckboxInput(attrs={'class': 'form-check-input', 'id': 'edit_athlete_para_athlete', 'name': 'edit_athlete_para_athlete'}),
'working_with_children': CheckboxInput(attrs={'class': 'form-check-input', 'id': 'edit_athlete_wwc_input', 'name': 'edit_athlete_wwc_input'}),
'expiry_date': DateInput(attrs={'type': 'date','class': 'form-control', 'id': 'edit_athlete_expiry_datepicker', 'name': 'edit_athlete_expiry_datepicker'}),
'available': CheckboxInput(attrs={'class': 'form-check-input', 'id': 'edit_athlete_available_input', 'name': 'edit_athlete_available_input'}),
'available_from': DateInput(attrs={'type': 'date','class': 'form-control', 'id': 'edit_athlete_datepicker', 'name': 'edit_athlete_datepicker'}),
'bfbw': CheckboxInput(attrs={'class': 'form-check-input', 'id': 'edit_athlete_bfbw_input', 'name': 'edit_athlete_bfbw_input'}),
'latest_bfbw_session': DateInput(attrs={'type': 'date','class': 'form-control', 'id': 'edit_athlete_bfbw_datepicker', 'name': 'edit_athlete_bfbw_datepicker'}),
'number_bfbw_sessions': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_bfbw_number_input', 'name': 'edit_athlete_bfbw_number_input'}),
'asp': CheckboxInput(attrs={'class': 'form-check-input', 'id': 'edit_athlete_asp_input', 'name': 'edit_athlete_asp_input'}),
'latest_asp_session': DateInput(attrs={'type': 'date','class': 'form-control', 'id': 'edit_athlete_asp_datepicker', 'name': 'edit_athlete_asp_datepicker'}),
'number_asp_sessions': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_asp_number_input', 'name': 'edit_athlete_asp_number_input'}),
'tours': CheckboxInput(attrs={'class': 'form-check-input', 'id': 'edit_athlete_tour_input', 'name': 'edit_athlete_tour_input'}),
'latest_tours_session': DateInput(attrs={'type': 'date','class': 'form-control', 'id': 'edit_athlete_tour_datepicker', 'name': 'edit_athlete_tour_datepicker'}),
'number_tours_sessions': TextInput(attrs={'class': 'form-control', 'id': 'edit_athlete_tour_number_input', 'name': 'edit_athlete_tour_number_input'}),
View
def update_athlete(request):
if request.method == 'POST':
athlete_ref_id = request.POST.get('athlete_ref')
ath_data = Athlete.objects.get(id=athlete_ref_id)
ath_data.athlete_name = request.POST['athlete_name']
ath_data.email = request.POST['email']
ath_data.home = request.POST['home']
ath_data.education = request.POST['education']
ath_data.sport = request.POST['sport']
ath_data.notes = request.POST['notes']
ath_data.gender = request.POST['gender']
ath_data.para_athlete = request.POST.get('para_athlete') == 'on'
ath_data.working_with_children = request.POST.get('working_with_children') == 'on'
ath_data.expiry_date = request.POST.get('expiry_date')
ath_data.available = request.POST.get('available') == 'on'
ath_data.available_from = request.POST.get('available_from')
ath_data.bfbw = request.POST.get('bfbw') == 'on'
ath_data.latest_bfbw_session = request.POST.get('latest_bfbw_session')
ath_data.number_bfbw_sessions = request.POST.get('number_bfbw_sessions')
ath_data.asp = request.POST.get('asp') == 'on'
ath_data.latest_asp_session = request.POST.get('latest_asp_session')
ath_data.number_asp_sessions = request.POST.get('number_asp_sessions')
ath_data.tours = request.POST.get('tours') == 'on'
ath_data.latest_tours_session = request.POST.get('latest_tours_session')
ath_data.number_tours_sessions = request.POST.get('number_tours_sessions')
ath_data.save()
print(ath_data.available)
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
HTML
<!-- EDIT Athlete Modal -->
<form method="post" action="update_athlete/" id="edit_athlete_form">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.source.errors }}
{{ form.source }}
<div class="modal fade" id="edit_athlete_modal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
<div class="modal-content">
<div class="modal-header card-header">
<h5 class="modal-title"><i class="fa fa-pencil mx-1"></i> Edit Athlete Details</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="card text-dark bg-light mb-3">
<div class="card-body">
<div class="form-floating mb-3">
{{edit_ath_form.athlete_name}}
<label for="edit_name">Name & Last Name</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.email}}
<label for="edit_email">Email address</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.phone_number}}
<label for="edit_athlete_phone_number">Phone number</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.home}}
<label for="edit_home">Home</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.education}}
<label for="edit_education">Education</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.sport}}
<label for="edit_sports">Sports</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.notes}}
<label for="edit_notes">Notes</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.gender}}
<label for="edit_gender">Gender</label>
</div>
<div class="form-check form-switch">
{{edit_ath_form.para_athlete}}
<label class="form-check-label" for="edit_athlete_para_athlete">Para athlete</label>
</div>
</div>
</div>
<div class="card text-dark bg-light mb-3">
<div class="card-body">
<div class="form-check form-switch">
{{edit_ath_form.working_with_children}}
<label class="form-check-label" for="edit_athlete_wwc_input">Has WWC</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.expiry_date}}
<label for="edit_athlete_expiry_datepicker">Expires on</label>
</div>
<div class="form-check form-switch">
{{edit_ath_form.available}}
<label class="form-check-label" for="edit_athlete_available_input">Available</label>
</div>
<div class="form-floating mb-3">
{{edit_ath_form.available_from}}
<label for="edit_athlete_datepicker">Available from</label>
</div>
<div class="form-check form-switch">
{{edit_ath_form.bfbw}}
<label class="form-check-label" for="edit_athlete_bfbw_input">BFBW</label>
</div>
<div class="row">
<div class="col-6">
<div class="form-floating mb-3">
{{edit_ath_form.latest_bfbw_session}}
<label for="edit_athlete_bfbw_datepicker">Latest BFBW Session</label>
</div>
</div>
<div class="col-6">
<div class="form-floating mb-3">
{{edit_ath_form.number_bfbw_sessions}}
<label for="edit_athlete_bfbw_number_input">Number of BFBW Sessions</label>
</div>
</div>
</div>
<div class="form-check form-switch">
{{edit_ath_form.asp}}
<label class="form-check-label" for="edit_athlete_asp_input">ASP</label>
</div>
<div class="row">
<div class="col-6">
<div class="form-floating mb-3">
{{edit_ath_form.latest_asp_session}}
<label for="edit_athlete_asp_datepicker">Latest ASP Session</label>
</div>
</div>
<div class="col-6">
<div class="form-floating mb-3">
{{edit_ath_form.number_asp_sessions}}
<label for="edit_athlete_asp_number_input">Number of ASP Sessions</label>
</div>
</div>
</div>
<div class="form-check form-switch">
{{edit_ath_form.tours}}
<label class="form-check-label" for="edit_athlete_tour_input">VIS Tours</label>
</div>
<div class="row">
<div class="col-6">
<div class="form-floating">
{{edit_ath_form.latest_tours_session}}
<label for="edit_athlete_tour_datepicker">Latest VIS Tour</label>
</div>
</div>
<div class="col-6">
<div class="form-floating mb-3">
{{edit_ath_form.number_tours_sessions}}
<label for="edit_athlete_tour_number_input">Number of Tours</label>
</div>
</div>
</div>
</div>
</div>
</div>
{{edit_ath_form.athlete_ref}}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save_edit_athlete">Save Changes</button>
</div>
</div>
</div>
</div>
</form>
<!-- END EDIT Athlete Modal -->
Fixed - Was a issue with JavaScript adding the false value.
Related
I am trying to create a form where you can enter the language or languages. I want to use a CheckboxSelectMultiple widget, but it renders already collapsed in the form:
The form with already collapsed select
How can I fix this?
forms.py:
class TweetForm(forms.ModelForm):
class Meta:
model = TweetSearch
fields = ['search_term', 'query_type', 'start_date', 'end_date', 'language', 'country', 'tweet_count']
widgets = {
'search_term': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Search...'}),
'query_type': forms.Select(attrs={'class': 'form-control'}),
'tweet_count': forms.NumberInput(attrs={'class': 'form-control', 'placeholder': 'Number of tweets'}),
'start_date': DateTimePickerInput(attrs={'class': 'form-control', 'placeholder': 'From'}),
'end_date': DateTimePickerInput(attrs={'class': 'form-control', 'placeholder': 'Till'}),
'language': forms.CheckboxSelectMultiple(attrs={'class':'form-control'})
}
html:
<div id="searchcontainer">
<div class="card w-50 text-center mx-auto mt-8" id="searchcard">
<div class="card-header">
Search
</div>
<div class="card-body">
<h5 class="card-title">Search for tweets</h5>
<form action="" method="POST">
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-4">
{{form.search_term}}
</div>
<div class="form-group col-md-4">
{{form.query_type}}
</div>
<div class="form-group col-md-4">
{{form.tweet_count}}
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
{{form.start_date}}
</div>
<div class="form-group col-md-6">
{{form.end_date}}
</div>
</div>
<div class="form-row">
{{form.language}}
</div>
<input class="btn btn-primary mb-2" type="submit">
</form>
</div>
</div>
Here you can see my models.py.
class Customer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name = 'customer', null=True, blank=True)
name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200)
tel = models.CharField(max_length=200)
def __str__(self):
return self.name
class Order(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True)
imya = models.CharField(max_length=200, null=False)
familiya = models.CharField(max_length=200, null=False)
tel = models.CharField(max_length=200, null=False)
address = models.CharField(max_length=200, null=False)
city = models.CharField(max_length=200, null=False)
state = models.CharField(max_length=200, null=False)
date_ordered = models.DateTimeField(auto_now_add=True)
complete = models.BooleanField(default=False)
transaction_id = models.CharField(max_length=100, null=True)
def __str__(self):
return str(self.id)
Here you can see my forms.py. Here I just created forms, but, the data should be got from template inputs.
from django import forms
from django.forms import ModelForm
from .models import *
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = '__all__'
views.py
def checkout(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
form = OrderForm()
if request.method == 'POST':
name = request.POST.get('name')
surname = request.POST.get('surname')
email = request.POST.get('email')
number = request.POST.get('number')
address = request.POST.get('address')
city = request.POST.get('city')
state = request.POST.get('state')
form = OrderForm(request.POST)
if form.is_valid():
Order.object.create(
imya=name,
familiya=surname,
tel=number,
adres=address,
city=city,
state=state,
)
form.save()
return HttpResponse('Заказ отправлен', safe=False)
context = {'items':items, 'order':order, 'cartItems':cartItems, 'form':form}
return render(request, 'store/checkout.html', context)
My template:
<section class="shop checkout section">
<div class="container">
<div class="row">
<div class="col-lg-8 col-12">
<div class="checkout-form">
<h2>Make Your Checkout Here</h2>
<p>Please register in order to checkout more quickly</p>
<!-- Form -->
<form class="form" method="post" action="#">
{% csrf_token %}
<div class="row">
<div class="col-lg-6 col-md-6 col-12">
<div class="form-group">
<label>First Name<span>*</span></label>
<input type="text" name="name" placeholder="" required="required">
</div>
</div>
<div class="col-lg-6 col-md-6 col-12">
<div class="form-group">
<label>Last Name<span>*</span></label>
<input type="text" name="surname" placeholder="" required="required">
</div>
</div>
<div class="col-lg-6 col-md-6 col-12">
<div class="form-group">
<label>Email Address<span>*</span></label>
<input type="email" name="email" placeholder="" required="required">
</div>
</div>
<div class="col-lg-6 col-md-6 col-12">
<div class="form-group">
<label>Phone Number<span>*</span></label>
<input type="number" name="number" placeholder="" required="required">
</div>
</div>
<div class="col-lg-6 col-md-6 col-12">
<div class="form-group">
<label>Address<span>*</span></label>
<input type="text" name="adress" placeholder="" required="required">
</div>
</div>
<div class="col-lg-6 col-md-6 col-12">
<div class="form-group">
<label>City<span>*</span></label>
<input type="text" name="city" placeholder="" >
</div>
</div>
<div class="col-lg-6 col-md-6 col-12">
<div class="form-group">
<label>State<span>*</span></label>
<input type="text" name="state" placeholder="" required="required">
</div>
</div>
<div class="col-12">
<div class="form-group create-account">
<input id="cbox" type="checkbox">
<label>Create an account?</label>
</div>
</div>
<input type="submit" value="Продолжить">
</div>
</form>
In my views, I want to create an object for my order model. I tried this code, It's not giving any errors, but it's not working. PLease, help with saving the order.
new to Django and Python here, building my first project.
I´m making a form for registering new user and I´m controlling my fields in a forms.py file
class UserRegisterForm(forms.ModelForm):
password1 = forms.CharField(
label='Contraseña',
required=True,
widget=forms.PasswordInput(
attrs={
# 'placeholder': 'Contraseña',
'class': 'input-group-field',
'oninvalid': "this.setCustomValidity('Ingrese una contraseña')",
'oninput': "setCustomValidity('')"
}
)
)
password2 = forms.CharField(
label='Contraseña',
required=True,
widget=forms.PasswordInput(
attrs={
# 'placeholder': 'Repetir Contraseña',
'class': 'input-group-field',
'oninvalid': "this.setCustomValidity('Repita la contraseña')",
'oninput': "setCustomValidity('')"
}
)
)
class Meta:
"""Meta definition for Userform."""
model = User
fields = (
'user_name',
'full_name',
'phone',
'email',
'role',
'gender',
'date_birth',
)
widgets = {
'user_name': forms.TextInput(
attrs={
# 'placeholder': 'Usuario ...',
'class': 'input-group-field',
'max_length': '20',
'oninvalid': "this.setCustomValidity('Ingrese un nombre de usuario')",
'oninput': "setCustomValidity('')"
}
),
'full_name': forms.TextInput(
attrs={
# 'placeholder': 'Nombres ...',
'class': 'input-group-field',
'max_length': '100',
'oninvalid': "this.setCustomValidity('Ingrese su nombre completo')",
'oninput': "setCustomValidity('')"
}
),
'phone': forms.TextInput(
attrs={
# 'placeholder': 'Teléfono ...',
'class': 'input-group-field',
'max_length': '50',
'oninvalid': "this.setCustomValidity('Ingrese su teléfono')",
'oninput': "setCustomValidity('')"
}
),
'email': forms.EmailInput(
attrs={
# 'placeholder': 'Correo Electronico ...',
'class': 'input-group-field',
'max_length': '100',
'blank': True,
'oninvalid': "this.setCustomValidity('Ingrese un mail válido')",
'oninput': "setCustomValidity('')"
}
),
'role': forms.Select(
attrs={
# 'placeholder': 'Rol ...',
'class': 'input-group-field',
'required': True,
}
),
'gender': forms.Select(
attrs={
# 'placeholder': 'Género ...',
'class': 'input-group-field',
}
),
'date_birth': forms.DateInput(
format='%Y-%m-%d',
attrs={
'type': 'date',
'class': 'input-group-field',
},
),
}
def clean_password2(self):
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
self.add_error('password2', 'Las contraseñas no son iguales')
Everything is working fine showing this kind of "error messages" in my html
So I have a method for clean password2, to check is the same as password 1 (Password confirmation), and I want the error message, to be the same format as the other fields.
Is this possible?
Thanks for your help!
I also attach here my template
{% extends "panel.html" %}
{% load static %}
{% block panel-content %}
<div class="grid-x medium-10">
<h3 class="cell medium-12" style="text-align: center;color: wheat;">Nuevo Usuario</h3>
<div class="cell medium-12">  </div>
<div class="cell medium-12 grid-x">
<div class="cell medium-2"></div>
<form class="cell medium-8 grid-x" method="POST">{% csrf_token %}
<div class="cell medium-4">
<label>Usuario:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-torso"></i></span>
{{ form.user_name }}
</div>
</div>
<div class="cell medium-9"></div>
<div class="cell medium-4">
<label>Nombre:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-book"></i></span>
{{form.full_name}}
</div>
</div>
<div class="cell medium-1"></div>
<div class="cell medium-3">
<label>Género:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-male-female"></i></span>
{{form.gender}}
</div>
</div>
<div class="cell medium-1"></div>
<div class="cell medium-3">
<label>Fecha de Nacimiento:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-calendar"></i></span>
{{ form.date_birth }}
</div>
</div>
<div class="cell medium-4">
<label>Email:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-mail"></i></span>
{{form.email}}
</div>
</div>
<div class="cell medium-1"></div>
<div class="cell medium-3">
<label>Teléfono:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-telephone"></i></span>
{{form.phone}}
</div>
</div>
<div class="cell medium-4"></div>
<div class="cell medium-4">
<label>Rol:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-star"></i></span>
{{form.role}}
</div>
</div>
<div class="cell medium-8"></div>
<div class="cell medium-4">
<label>Contraseña:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-key"></i></span>
{{ form.password1 }}
</div>
</div>
<div class="cell medium-1"></div>
<div class="cell medium-4">
<label>Repetir Contraseña:</label>
<div class="input-group">
<span class="input-group-label"><i class="fi-key"></i></span>
{{ form.password2 }}
</div>
</div>
<!-- {% for error in form.password2.errors %}
<p class="cell" style="color: red;">
{{ error|escape }}
</p>
{% endfor %} -->
<div class="cell medium-3"></div>
<div class="cell medium-2">
<button type="submit" class="cell success button "> <i class="fi-save"></i>  Guardar</button>
</div>
</form>
<div class="cell medium-2"></div>
</div>
</div>
{% endblock panel-content %}
I think you might be interested in this answer : https://stackoverflow.com/a/54020111/8439435
Basically you would need to write a custom javascript function to check that both password match each other and use setCustomValidity to show a browser error message on validation of the form if passwords are different.
I have issue might missed something , i have created CreateView view for submitting objects in db , all seems to ok , but when i try to submit i don't get anything happen no error at all except
"POST /create_task/ HTTP/1.1" 200 12972 ,
MY code goes as follows , please advice
Thanks
models.py
class MainTask(models.Model):
task_title = models.CharField(max_length=200)
global_task_info = models.TextField(max_length=500,default=None)
complete = models.BooleanField(default=False)
overall_precent_complete = models.PositiveIntegerField(default=0)
created_at = models.DateTimeField(default=datetime.datetime.now())
updated_at = models.DateTimeField(default=datetime.datetime.now())
due_date = models.DateTimeField(default=datetime.datetime.now())
task_location = models.CharField(max_length=400, blank=True, null=True)
global_task_assign = models.ForeignKey(UserProfile, on_delete=models.CASCADE, related_name="global_task_assign",default=1)
TASK_STATUS_CHOICES = [
('ST', 'STARTED'),
('NS', 'NOT STARTED'),
('IP', 'IN PROGRESS'),
('PA', 'PAUSED'),
('CO', 'COMPLETED'),
]
task_status = models.CharField(max_length=2,choices=TASK_STATUS_CHOICES,default='NOT STARTED')
def __str__(self):
return self.task_title
forms.py
class TaskCraetionForm(forms.ModelForm):
TASK_STATUS_CHOICES = [
('ST', 'STARTED'),
('NS', 'NOT STARTED'),
('IP', 'IN PROGRESS'),
('PA', 'PAUSED'),
('CO', 'COMPLETED'),
]
task_title = forms.CharField(max_length=200, widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Task Title'}))
global_task_info = forms.CharField(max_length=500, widget=forms.Textarea(attrs={'class':'form-control','placeholder':'Task Description'}))
due_date = forms.DateTimeField(widget=forms.DateTimeInput(attrs={
'class': 'form-control',
'id': 'picker'
}))
global_task_assign = forms.ModelChoiceField(queryset= UserProfile.objects.all(), widget=forms.Select(attrs={'class':'form-control'} ))
task_status = forms.ChoiceField(label='', choices=TASK_STATUS_CHOICES, widget=forms.Select(attrs={'class':'form-control'}))
class Meta:
model = MainTask
fields = ['task_title',
'global_task_info',
'due_date',
'global_task_assign',
'task_status',
]
views.py
class CreatTaskView(CreateView):
model = MainTask
template_name = "create_newtask.html"
form_class = TaskCraetionForm
success_url = None
def form_valid(self, form):
f = form.save(commit=False)
f.save()
return super(CreatTaskView, self).form_valid(form)
Thank you very much Alasdair you're comment gave me the direction and more added the following to my HTML template shown below and found out i have issue with my datetime picker format needed to added the following
Thanks
INPUTֹTIMEֹFORMATS = [
'%Y/%m/%d %H:%M']
due_date = forms.DateTimeField(input_formats=INPUTֹTIMEֹFORMATS, widget=forms.DateTimeInput(attrs={
'class': 'form-control',
'id': 'picker'
}))
html temaplate
<form action="" method="POST">
<h3 class="mt-3 text-left">Create New Task</h3>
<hr>
<p class="text-muted text-left">Creat New Itom task</p>
{% csrf_token %}
{% if form.errors %}
<!-- Error messaging -->
<div id="errors">
<div class="inner">
<p>There were some errors in the information you entered. Please correct the following:</p>
<ul>
{% for field in form %}
{% if field.errors %}<li>{{ field.label }}: {{ field.errors|striptags }}</li>{% endif %}
{% endfor %}
</ul>
</div>
</div>
<!-- /Error messaging -->
{% endif %}
<div class="input-group mt-3 mb-3 mr-auto">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1"><i class="fas fa-book-medical"></i></span>
</div>
{{ form.task_title}}
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-pen"></i></span>
</div>
{{form.global_task_info}}
</div>
<!---date time picker-->
<h6 class="text-left">Task Due Date</h6>
<div class="input-group date mb-3 col-3">
<div class="input-group-append">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
{{ form.due_date }}
</div>
<!--end of date time picker-->
<!---user assign-->
<h6 class="text-left">Assign Task to IT member</h6>
<div class="input-group mb-3 mt-3 col-8">
<div class="input-group-prepend">
<div class="input-group-text"><i class="fas fa-user-tie"></i></div>
{{form.global_task_assign}}
</div>
</div>
<!--End Of User Assign-->
<h6 class="text-left">Set Task Status</h6>
<div class="input-group mb-3 mt-3 col-4">
<div class="input-group-prepend">
<div class="input-group-text"><i class="far fa-caret-square-right"></i></div>
</div>
{{form.task_status}}
</div>
<div class="col text-left">
<button type="submit" value="Save" class="btn btn-primary btn-lg text-white mt-2"><span><i class="fas fa-database"></i></span> Create Task</button>
</div>
</form>
</div>
</div>
I'm developing the backend of my personal blog and, using this tutorial for the date and time, I've created a form for the creation of a blog post.
Even if I select one or more tags, they aren't added to the post. The post after the pubblication using this form doesen't have tags. But if I do the same thing via django admin I can't create a post without the tags.
create_post.html
<form class="" method="POST" enctype="multipart/form-data" novalidate>{% csrf_token %}
<div class="form-group">
<div class="row">
<div class="col-sm-9">
<div class="form-group mb-4">
<div>{{ form.title }}</div>
<label for="id_title">
<span class="text-info" data-toggle="tooltip" title="{{ form.title.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.title.errors }}</small>
</label>
</div>
<div class="form-group mb-4">
<div>{{ form.description }}</div>
<label for="id_description">
<span class="text-info" data-toggle="tooltip" data-placement="bottom" title="{{ form.description.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.description.errors }}</small>
</label>
</div>
<div class="form-group mb-4">
<div>{{ form.contents }}</div>
<label for="id_contents">
<span class="text-info" data-toggle="tooltip" data-placement="bottom" title="{{ form.contents.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.contents.errors }}</small>
</label>
</div>
<div class="form-group mb-4">
<div>{{ form.header_image_link }}</div>
<label for="id_header_image">
<span class="text-info" data-toggle="tooltip" title="{{ form.header_image_link.help_text|safe }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.header_image_link.errors }}</small>
</label>
</div>
</div>
<div class="col-sm-3">
<div class="form-group mb-4">
<div class=""><h4>{{ form.post_category.label }}</h4></div>
<div>{{ form.post_category }}</div>
<label for="id_category">
<span class="text-info" data-toggle="tooltip" title="{{ form.post_category.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.post_category.errors }}</small>
</label>
</div>
<div class="form-group mb-4">
<div class=""><h4>{{ form.post_tag.label }}</h4></div>
<div>{{ form.post_tag }}</div>
<label for="id_tag">
<span class="text-info" data-toggle="tooltip" title="{{ form.post_tag.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.post_tag.errors }}</small>
</label>
</div>
<div class="form-inline mb-4 py-0">
<div class="input-group mx-1">
<label for="id_highlighted">{{ form.highlighted.label }}</label>
<div class="ml-1">{{ form.highlighted }}</div>
</div>
<div class="input-group mx-1">
<label for="id_draft">{{ form.draft.label }}</label>
<div class="ml-1">{{ form.draft }}</div>
</div>
</div>
<div class="form-group mb-4">
<div class=""><h4>{{ form.publishing_date.label }}</h4></div>
<div class="input-group date" data-target-input="nearest">
{{ form.publishing_date }}
<div class="input-group-append" data-target="#publishing_date_field" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
<label for="publishing_date_field">
<span class="text-info" data-toggle="tooltip" title="{{ form.publishing_date.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.publishing_date.errors }}</small>
</label>
<script>
$(function () {
$("#publishing_date_field").datetimepicker({
format: 'DD/MM/YYYY HH:mm',
});
});
</script>
</div>
<div class="form-group mb-4">
<div class=""><h4>{{ form.author.label }}</h4></div>
<div>{{ form.author }}</div>
<label for="id_author">
<span class="text-info" data-toggle="tooltip" title="{{ form.author.help_text }}">
<i class="far fa-question-circle"></i>
</span>
<small class="text-danger">{{ form.author.errors }}</small>
</label>
</div>
</div>
</div>
</div>
<hr>
<div class="row justify-content-md-center">
<div class="col-md-auto">
<input type="submit" class="btn btn-info shadow" value="Pubblica">
</div>
</div>
</form>
forms.py
class BlogPostForm(forms.ModelForm):
title = forms.CharField(
max_length=70,
help_text="<small>Write post title here. The title must be have max 70 characters.</small>",
widget=forms.TextInput(
attrs={
"placeholder": "Titolo",
"type": "text",
"id": "id_title",
"class": "form-control form-control-lg",
}
),
)
description = forms.CharField(
max_length=200,
help_text="<small>Write a post short description here. The description must be have max 200 characters.</small>",
widget=forms.Textarea(
attrs={
"placeholder": "Descrizione",
"type": "text",
"id": "id_description",
"class": "form-control",
"rows": "2",
}
),
)
contents = forms.CharField(
help_text="<small>Write your contents here.</small>",
widget=forms.Textarea(
attrs={
"placeholder": "Contenuti",
"type": "text",
"id": "id_contents",
"class": "form-control",
"rows": "20",
}
),
)
publishing_date = forms.DateTimeField(
input_formats=['%d/%m/%Y %H:%M'],
label="Data di pubblicazione",
help_text="<small>Write data and hour of publication. You can use also a past or a future date.</small>",
widget=forms.DateTimeInput(
attrs={
"id": "publishing_date_field",
'class': 'form-control datetimepicker-input',
'data-target': '#publishing_date_field',
}
),
)
draft = forms.BooleanField(
label="Bozza",
widget=forms.CheckboxInput(
attrs={
"type": "checkbox",
"id": "id_draft",
"class": "form-check-input mx-2",
}
),
required=False,
)
author = forms.ModelChoiceField(
label="Autore",
help_text="<small>Select the author.</small>",
widget= forms.Select(
attrs={
"id": "id_author",
"class": "custom-select",
}
),
empty_label=" ",
queryset= User.objects.all(),
)
post_tag =forms.ModelMultipleChoiceField(
label="Tag",
help_text="<small>Select one or more tags. Press CTRL and click on a tag for multiple selection.</small>",
widget= forms.SelectMultiple(
attrs={
"id": "id_category",
"class": "custom-select",
"size": "4",
}
),
queryset= BlogTag.objects.all(),
)
post_category = forms.ModelChoiceField(
label="Categoria",
help_text="<small>Select a category.</small>",
widget= forms.Select(
attrs={
"id": "id_category",
"class": "custom-select",
}
),
empty_label=" ",
queryset= BlogCategory.objects.all(),
)
header_image_link = forms.CharField(
max_length=1000,
help_text="<small>Past here the image link.</small>",
widget=forms.TextInput(
attrs={
"placeholder": "Url dell'immagine di testata",
"type": "text",
"id": "id_header_image",
"class": "form-control",
}
)
)
highlighted = forms.BooleanField(
label="In evidenza",
widget=forms.CheckboxInput(
attrs={
"type": "checkbox",
"id": "id_highlighted",
"class": "form-check-input mx-2",
}
),
required=False,
)
class Meta:
model = BlogPost
fields = [
"author",
"title",
"description",
"contents",
"header_image_link",
"post_tag",
"post_category",
"highlighted",
"draft",
"publishing_date",
]
models.py
class BlogPost(models.Model):
publishing_date = models.DateTimeField(
default=timezone.now,
blank=True,
)
updating_date = models.DateTimeField(
auto_now=True,
)
timestamp = models.DateTimeField(
auto_now=False,
auto_now_add=True,
)
title = models.CharField(
max_length=70,
unique=True,
)
slug_post = models.SlugField(
max_length=70,
unique=True,
)
description = models.TextField(
max_length=200,
blank=True,
)
contents = models.TextField(
blank=True,
)
draft = models.BooleanField(
default=False,
)
author = models.ForeignKey(
User,
related_name = "author_blogpost",
on_delete=models.CASCADE,
)
highlighted = models.BooleanField(
default=False,
)
header_image_link = models.CharField(
max_length=1000,
)
post_category = models.ForeignKey(
BlogCategory,
related_name = "category_blogpost",
on_delete=models.CASCADE,
)
post_tag = models.ManyToManyField(
BlogTag,
related_name = "tag_blogpost",
)
def __str__(self):
return self.title
class Meta:
ordering = ['-publishing_date']
views.py
def createPost(request):
if request.method == "POST":
form = BlogPostForm(request.POST or None)
if form.is_valid():
new_post = form.save(commit=False)
new_post.slug_post = slugify(new_post.title)
new_post.save()
return redirect('blogpost_list')
else:
form = BlogPostForm()
context = {
'form': form,
}
template = 'blog/editing/create_post.html'
return render(request, template, context)
def updatePost(request, slug_post=None):
update_post = get_object_or_404(BlogPost, slug_post=slug_post)
form = BlogPostForm(request.POST or None, instance=update_post)
if form.is_valid():
update_post = form.save(commit=False)
update_post.slug_post = slugify(update_post.title)
update_post.save()
return redirect('blogpost_list')
context = {
'form': form,
}
template = 'blog/editing/create_post.html'
return render(request, template, context)
What I've wrong?
Many to many relationships have to be manually set. You can either override your ModelForm's save method, this would mean that you can never use "commit=False"
def save(self, commit=True):
if not commit:
raise Exception('Cannot save many to many fields if the blog post is not committed')
instance = super().save()
instance.post_tag.add(*self.cleaned_data['post_tag'])
return instance
or you can add the tags in your view
if form.is_valid():
update_post = form.save(commit=False)
update_post.slug_post = slugify(update_post.title)
update_post.save()
update_post.post_tag.add(*form.cleaned_data['post_tag'])
return redirect('blogpost_list')