Appear data in URL form bu using Django - html

I want to create a record and want to appear data in the form fields. How can I do that ? Do I need to write javascript for it. If you help me, really apprepriate it. Thanks for now.
Here is models.py;
class hesaplarim(models.Model):
hesap_id = models.AutoField(primary_key=True)
tc_no = models.IntegerField(unique=True)
name = models.CharField(max_length=55)
surname = models.CharField(max_length=55)
phone_number = models.IntegerField()
gender = models.CharField(max_length=5)
Here is views.py;
def home(request):
form = HesapForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = HesapForm()
return render(request, 'home.html', {'form': form})
Here is forms.py;
class HesapForm(forms.ModelForm):
ERKEK = 'ERKEK'
KADIN = 'KADIN'
gender_choices = (
(ERKEK, 'ERKEK'),
(KADIN, 'KADIN')
)
tc_no = forms.IntegerField(widget=forms.NumberInput)
name = forms.CharField(widget=forms.TextInput)
surname = forms.CharField(widget=forms.TextInput)
phone_number = forms.IntegerField(widget=forms.NumberInput)
gender = forms.ChoiceField(choices=gender_choices)
class Meta:
model = hesaplarim
fields = ('tc_no', 'name', 'surname', 'phone_number',
'gender')
Here is html file;
<form method="post" class="form-horizontal" novalidate>
{% csrf_token %}
<div class="form-group">
<label for="id_tc_no">TC No:</label>
{{ form.tc_no }}
</div>
<div class="form-group">
<label for="id_name">Ad:</label>
{{ form.name }}
</div>
<div class="form-group">
<label for="id_surname">Soyad:</label>
{{ form.surname }}
</div>
<div class="form-group">
<label for="id_phone">Cep Telefonu:</label>
{{ form.phone_number }}
</div>
<div class="form-group">
<label for="id_gender">Cinsiyet:</label>
{{ form.gender }}
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Kaydet">
</div>
</form>

You need to initialize the form:
def home(request):
form = HesapForm(request.POST or None)
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = HesapForm(initial={'your_field': 'your_value'})
return render(request, 'home.html', {'form': form})
Dynamic initial values you can refer about the same here.
You can also initialize it with object as well.

Related

Django ChoiceField with custom forms

I am relatively new to Django and have been following up some tutorials on how to create registration forms with Django.
This is the html form code
<form action="{% url 'register' %}" method="post">
{% csrf_token %}
<div class="form-row">
<div class="col form-group">
<label>First name</label>
{{ form.first_name }}
</div> <!-- form-group end.// -->
<div class="col form-group">
<label>Last name</label>
{{ form.last_name }}
</div> <!-- form-group end.// -->
</div> <!-- form-row end.// -->
<div class="form-row">
<div class="col form-group">
<label>Email</label>
{{ form.email }}
</div> <!-- form-group end.// -->
<div class="form-group col-md-6">
<label>Directorate</label>
<select name="directorate" class="form-control" required>
<option value="" disabled selected>Select</option>
{% for i in form.directorate %}
<option value=""></option>
{% endfor %}
</select>
</div> <!-- form-group end.// -->
</div> <!-- form-row end.// -->
<div class="form-row">
<div class="form-group col-md-6">
<label>Create password</label>
{{ form.password }}
</div> <!-- form-group end.// -->
<div class="form-group col-md-6">
<label>Repeat password</label>
{{ form.confirm_password }}
</div> <!-- form-group end.// -->
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block"> Register </button>
</div> <!-- form-group// -->
</form>
models.py
class Account(AbstractBaseUser):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
username = models.CharField(max_length=50, unique=True)
email = models.EmailField(max_length=100, unique=True)
directorate = models.CharField(max_length=100)
# required
date_joined = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(auto_now_add=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
is_superadmin = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'first_name', 'last_name']
objects = MyAccountManager()
views.py
from django.http import HttpResponse
from django.shortcuts import render
from .forms import RegistrationForm
from .models import Account
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
email = form.cleaned_data['email']
username = email.split('#')[0]
password = form.cleaned_data['password']
directorate = form.cleaned_data['directorate']
user = Account.objects.create_user(first_name=first_name, last_name=last_name, email=email, directorate=directorate, username=username, password=password)
user.save()
else:
form = RegistrationForm()
context = {
'form': form,
}
return render(request, 'accounts/register.html', context)
forms.py
from django import forms
from .models import Account
DIRECTORATE_CHOICES = [
("E", "Education"),
("H", "Health"),
("T", "Transport"),
]
class DirectorateChoices(forms.Form):
directorate = forms.ChoiceField(choices=DIRECTORATE_CHOICES,widget=forms.Select(attrs={
'class':'form-control'}), required=True)
class RegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Enter your password',
}))
confirm_password = forms.CharField(widget=forms.PasswordInput(attrs={
'placeholder': 'Confirm your password',
}))
class Meta:
model = Account
fields = ['first_name', 'last_name', 'email', 'directorate', 'password']
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.fields['first_name'].widget.attrs['placeholder'] = 'First name'
self.fields['last_name'].widget.attrs['placeholder'] = 'Last name'
self.fields['email'].widget.attrs['placeholder'] = 'Email address'
self.fields['directorate'].widget.attrs['placeholder'] = 'Select your directorate'
self.fields['password'].widget.attrs['placeholder'] = 'Enter password'
self.fields['confirm_password'].widget.attrs['placeholder'] = 'Confirm password'
for field in self.fields:
self.fields[field].widget.attrs['class'] = 'form-control'
I however want to introduce a ChoiceField on my registration form but from the reading and Youtube guide's I have followed, the field that is supposed to be loading the choices is not loading. This is my registration form

Modifying a dropdown menu option in a form Django

I have a dropdown menu that i'm using to fill a field on a form, but, as i'm just starting with django and html, i'm using another form to edit and update the data of the previous form. But i cant make the dropdown menu to work on that second form.
The model:
class Orden(models.Model):
STATUS = (
('En espera', 'En espera'),
('En proceso', 'En proceso'),
('Terminado', 'Terminado'),
('Entregado', 'Entregado'),
)
num_orden = models.CharField(max_length=20, default='')
fechain = models.CharField(max_length=30, default='')
fechaout = models.CharField(max_length=30, default='')
instrumento = models.CharField(max_length=30, default='')
marca = models.CharField(max_length=20)
referencia = models.CharField(max_length=30, default='')
serial = models.CharField(max_length=15, default='')
encargado = models.CharField(max_length=50, default='')
abono = models.CharField(max_length=15, default='')
procesos = models.TextField(default='')
estado = models.CharField(max_length=50, null=True, choices=STATUS) # <--- This one
client = models.ForeignKey(Cliente, on_delete=models.CASCADE, default='', related_name="client")
The view related to the first form:
def ordenfill(request, client_id):
if request.method == 'POST':
orden = ordenForm(request.POST)
if orden.is_valid():
orden.instance.client_id = client_id
init = orden.save()
return redirect('ordenview', id=init.id)
else:
orden = ordenForm()
client = Cliente.objects.get(id=client_id)
context = {
'orden': orden,
'client': client,
}
return render(request, 'ordenfill.html', context)
The template part asociated to that dropdown menu:
<form method="POST" class="post-form" action="{% url 'ordenfill' client.id %}">
{% csrf_token %}
<div class="container">
<br>
.
.
.
<div class="form-group row">
<label class="col-sm-2 col-form-label">Estado</label>
<div class="col-sm-4">
{{orden.estado}}
</div>
</div>
</div>
</form>
Image of the First Form
Dropdown menu on the first form
The view related to the second form:
def ordenupd(request, id):
orden = Orden.objects.get(id=id)
status = Orden.STATUS
context = {
'orden': orden,
'status': status,
}
return render(request, "ordenupd.html", context)
Edit: the view that saves the changes in the second form
def ordenmod(request, id):
orden = get_object_or_404(Orden, id=id)
if request.method == 'GET':
orden = ordenForm(instance=orden)
return redirect("ordenview", id=id)
else:
orden = ordenForm(request.POST, instance=orden)
print(orden.errors)
if orden.is_valid():
orden.save()
return redirect("ordenview", id=id)
else:
orden = clienteForm()
return redirect("ordenview", id=id)
The template part asociated to the dropdown menu of that view:
<form method="POST" class="post-form" action="/ordenmod/{{orden.id}}">
<input type="hidden" name="id" id="id" required maxlength="20" value="{{orden.id}}"/>
{% csrf_token %}
<div class="container">
<br>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Estado</label>
<div class="col-sm-4">
<select name="select_path" id="select_path">
<option value="{{orden.estado}}">{{orden.estado}}</option>
{% for items in status %}
<option value ="{{orden.estado}}">{{orden.estado}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
</form>
And the webpage of that view:
Dropdown menu on the second form
How can i make that dropdown menu on the second form to work like the one on the first form?
#chnage your view as below
from .models import STATUS
def ordenupd(request, id):
status = STATUS
context = {
'status': status,
}
return render(request, "ordenupd.html", context)
#change your HTML form template as below
<form method="POST" class="post-form" action="/ordenmod/{{orden.id}}">
<input type="hidden" name="id" id="id" required maxlength="20" value="{{orden.id}}"/>
{% csrf_token %}
<div class="container">
<br>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Estado</label>
<div class="col-sm-4">
<select name="select_path" id="select_path">
{% for items in status %}
<option value ="{{item.1}}">{{item.1}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
</form>

how to show data through input tag

I am working on a Django project .In front-end I want to add user data through input tags but I do not know how to save data through input tags
My models.py file is::
class User(AbstractBaseUser):
full_name = models.CharField(max_length=255, blank=True, null=True)
sur_name = models.CharField(max_length=255, blank=True, null=True)
email = models.EmailField(max_length=255 ,unique=True)
choose_subject = models.CharField(choices=SUBJECT_CHOICES , max_length=100)
staff = models.BooleanField(default=False)
admin = models.BooleanField(default=False)
time_stamp = models.DateTimeField(auto_now_add=True)
father_name = models.CharField(max_length=255)
father_sur_name = models.CharField(max_length=255)
mobile_phone_number = models.IntegerField(blank=True,null=True)
father_email = models.EmailField(max_length=255, unique=True)
fiscal_code = models.CharField(max_length=20)
address = models.CharField(max_length=200 )
A part of my register.html file is:
<div class="card-body">
<h2 class="title">Registration Form</h2>
<form method="POST">{% csrf_token %}
<div class="row row-space">
<div class="col-2">
<div class="input-group">
<label class="label">Full name</label>
<input class="input--style-4" type="text" name="first_name"{{form.full_name}}>
</div>
</div>
<div class="col-2">
<div class="input-group">
<label class="label">Sur name</label>
<input class="input--style-4" type="text" name="last_name"{{form.sur_name}}>
</div>
</div>
</div>
I just want the Django model field to be working in input tags
You must create in the views.py file a method in which the templates and the logic of it, an example can be the following
views.py
#csrf_exempt
def register(request):
if request.method == 'POST':
var1 = request.POST.get('var1')
var2 = request.POST.get('var2')
var3 = request.POST.get('var3')
#Save to the database here
return render_to_response(
'home.html',
{'message': 'Update Success', }
)
else:
#elif request.method == "GET":
obj = table.objects.all()
if obj:
ctx['data'] = obj
return render(request, template_name, ctx)
and in your template add a form tag
<form method="POST">
{ % csrf_token % }
<div class="row row-space">
<div class="col-2">
<div class="input-group">
<label class="label">Full name</label>
<input class="input--style-4" type="text" value={{data.full_name}}>
</div>
</div>
<div class="col-2">
<div class="input-group">
<label class="label">Sur name</label>
<input class="input--style-4" type="text" value={{data.sur_name}}>
</div>
</div>
</div>
<input type="submit" value="OK">
</form>
refer this tutorial

Why doesn't the Django formset appear?

I have one form and one formset on the same page. Prior to my deletion of information from django admin, I could see both the form and the formset. After I deleted some 'patient' information and updated my css file, the formset stopped showing.
views.py
def patient_new(request):
patientForm = PatientForm()
formset = LocationFormSet()
if request.method == "POST":
patientForm = PatientForm(request.POST)
formset = LocationFormSet(request.POST)
if patientForm.is_valid() and formset.is_valid():
patient = patientForm.save(commit=True)
for form in formset:
location = form.save(commit=False)
location.patient = patient
location.save()
return index(request)
else:
print('ERROR FORM INVALID')
return render(request, 'covidapp/patient_new.html',{'patientForm':patientForm, 'formset':formset})
html file
<form class="form-horizontal" method="POST">
{% csrf_token %}
<div class="jumbotron">
<div class="row spacer">
<div class="col-2">
<div class="input-group">
{{ patientForm.as_p }}
</div>
</div>
</div>
{{ formset.management_form }}
{% for form in formset %}
<div class="row form-row spacer">
<div class="col-6">
<div class="input-group">
{{ form.as_p }}
<div class="input-group-append">
<button type="button" class="btn btn-success add-form-row">+</button>
</div>
</div>
</div>
</div>
{% endfor %}
<div class="row spacer">
<div class="col-4 offset-2"></div>
<button type="submit" class="btn btn-block btn-primary">Create</button>
</div>
</div>
</form>
html output
I am not sure whether the hidden input is supposed to be my formset and why it is hidden.
model.py
class Patient(models.Model):
name = models.CharField(max_length=200)
idn = models.IntegerField(unique=True)
date_of_birth = models.DateField()
date_of_confirm = models.DateField()
case_number = models.IntegerField()
def get_absolute_url(self):
return reverse("patient_detail", kwargs={'pk':self.pk})
def __str__(self):
return self.name
class Location(models.Model):
patient = models.ForeignKey(Patient, related_name='locations', on_delete=models.CASCADE)
location_name = models.CharField(max_length=50, null=True)
address = models.CharField(max_length=300, null=True)
grid_x = models.IntegerField(null=True)
grid_y = models.IntegerField(null=True)
date_from = models.DateField(null=True)
date_to = models.DateField(null=True)
details = models.CharField(max_length=300, null=True)
category = models.CharField(max_length=300, null=True)
def __str__(self):
return self.location_name
form.py
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = '__all__'
LocationFormSet = modelformset_factory(
Location,
extra=0,
exclude = ('patient',)
)

Form error in a Django form with Bootstrap tagsinput plugin

I am using Bootstrap/Tagsinput for adding tags in a Django input form.
Problem
The first field is where I want the tags added and the second field is a simple text field. I am able to add the tags in the first field and a normal text in the second field. But, when I submit the form the input values get removed and am returned to the original form. The django form.errors indicates that the second field is empty with a This field is required message even though I had entered a text! What am I missing?
form.errors
Is returning
<ul class="errorlist"><li>experiments<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
Here are the related files
tags_add.html
I am using a custom html rather than the django's forms.as_table. All required css and min.js have been added in the html file.
<form id="defaultForm" method="post" action="" class="form-horizontal">{% csrf_token %}
<div class="form-group">
<label class="col-lg-3 control-label">Tags:</label>
<div class="col-lg-5">
<input type="text" name="tags" id="aa" class="form-control" value="" data-role="tagsinput" />
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Experiments:</label>
<div class="col-lg-5">
<input type="text" name="tags" id="exp_id" class="form-control" value="" />
</div>
</div>
<div class="form-group">
<div class="col-xs-5 col-xs-offset-3">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
forms.py
class TagsForm(ModelForm):
tags = forms.CharField( widget=forms.TextInput(attrs={'size':'20', 'placeholder':'add tags'}), required=True )
experiments = forms.CharField( widget=forms.TextInput(attrs={'size':'20', 'placeholder':'expid'}), required=True )
def __init__(self, *args, **kwargs):
super(TagsForm, self).__init__(*args, **kwargs)
class Meta:
model = Tags
fields = '__all__'
views.py
def view_tags(request,formtype):
form_type = formtype
if request.method == "POST":
form = form_type(request.POST or None)#, extra=extra_additives)
if form.is_valid():
instances = form.save(commit=False)
temp_str= str(instances)
print temp_str
experiment_tags = temp_str
return HttpResponse(experiment_tags)
else:
print "from view_tags {0}".format(form.errors)
return HttpResponse(form.errors)
else:
form = TagsForm()
return render(request, 'tags_add.html', {'form': form})
models.py
class Tags(models.Model):
tags = models.CharField(max_length=1000)
experiments = models.CharField(max_length=1000)
def __str__(self):
return ':'.join([self.tags, self.experiments])
I got it working by adding the following to a scripts tag in the html file:
$("input[name='tags']").tagsinput({
confirmKeys:[13] //-- Only enter
});