在Django中如何在不使用form.py的情况下从html表添加数据 [英] in django how to add data from html table without using form.py
本文介绍了在Django中如何在不使用form.py的情况下从html表添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不使用form.py的情况下从Django中的html表中保存数据.我目前正在使用添加按钮在html中创建表,并且在添加了表中的所有行后我想保存它,但我没有使用form.py仅view.py,html,model.py我的查看代码如下views.py
how to save data from html table in django without using form.py. I am currently creating table in html with add button and after adding all rows in table i want to save it but i am not using form.py only view.py,html,model.py my view code is below views.py
school_name = request.POST['school_name']
m_pass_out = request.POST['m_pass_out']
medicalschool = MedicalSchool(school_name=school_name, m_pass_out=m_pass_out)
medicalschool.save()
我的型号代码如下models.py
my model code is below models.py
class DoctorProfile(models.Model):
user_guid = models.OneToOneField(
'EgdEradUsers', models.DO_NOTHING, db_column='user_guid', primary_key=True)
doctor_guid = models.UUIDField(unique=True)
featured_doctor_id = models.BooleanField()
primary_speciality = models.ForeignKey(DSpecialtyType, models.DO_NOTHING)
# This field type is a guess.
secondary_speciality = models.TextField(blank=True, null=True)
years_experience = models.IntegerField()
# This field type is a guess.
education = models.TextField(blank=True, null=True)
license_number = models.CharField(max_length=1000, blank=True, null=True)
npi_number = models.CharField(max_length=1000, blank=True, null=True)
revalidation_cme = models.IntegerField(blank=True, null=True)
# This field type is a guess.
states_to_practice = models.TextField(blank=True, null=True)
# This field type is a guess.
board_certification = models.TextField(blank=True, null=True)
# This field type is a guess.
honors_awards_recognition = models.TextField(blank=True, null=True)
# This field type is a guess.
publications = models.TextField(blank=True, null=True)
description = models.CharField(max_length=1000, blank=True, null=True)
# This field type is a guess.
hospital_privileges = models.TextField(blank=True, null=True)
phone_code = models.IntegerField(blank=True, null=True)
primary_contact_number = models.CharField(max_length=45)
phone_code2 = models.IntegerField(blank=True, null=True)
secondary_contact_number = models.CharField(
max_length=45, blank=True, null=True)
resume_url = models.CharField(max_length=1000, blank=True, null=True)
avatar_url = models.CharField(max_length=1000, blank=True, null=True)
additional_comments = models.CharField(
max_length=1000, blank=True, null=True)
class Meta:
managed = True
db_table = 'doctor_profile'
class MedicalSchool(models.Model):
school_name = models.CharField(max_length=100)
m_pass_out = models.DateField(max_length=100)
doctor_profile = models.ForeignKey(DoctorProfile, on_delete=models.CASCADE)
created_at = models.DateTimeField()
updated_at = models.DateTimeField(blank=True, null=True)
class Meta:
db_table = 'medical_school'
我的html代码在下面html
my html code is below html
<div class="container-lg">
<div class="table-responsive">
<div class="table-wrapper">
<div class="table-title">
<div class="row">
<div class="col-sm-8">
<h2>Medical School</h2>
</div>
<div class="col-sm-4">
<button type="button" id="medical" class="btn btn-info add-
new"><i class="fa fa-plus"></i> Add New</button>
</div>
</div>
</div>
<table id="medicaltable" class="table table-bordered">
<thead>
<tr>
<th>Name of School</th>
<th>Year of Graduation</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td id="medicaltext" name="school_name"></td>
<td id="medicaldate" name="m_pass_out"></td>
<td>
<a id="medicaladd" class="add" title="Add" data-
toggle="tooltip"><i class="material-icons"></i></a>
<a id="medicaledit" class="edit" title="Edit" data-
toggle="tooltip"><i class="material-icons"></i></a>
<a id="medicaldelete" class="delete" title="Delete" data-
toggle="tooltip"><i class="material-icons"></i></a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
推荐答案
,您可以在视图中按其名称访问每个html表单输入.参见下面的代码:
you can access to your each html form inputs by their name in view. see below code:
模型
from django.db import models
from django import forms
class MedicalSchool(models.Model):
school_name = models.CharField(max_length=100)
m_pass_out = models.DateField(max_length=100)
doctor_profile = models.ForeignKey(DoctorProfile, on_delete=models.CASCADE)
created_at = models.DateTimeField()
updated_at = models.DateTimeField(blank=True, null=True)
class Meta:
db_table = 'medical_school'
class MedicalSchoolForm(forms.ModelForm):
class Meta:
model = MedicalSchool
fields = ['school_name', 'm_pass_out', 'doctor_profile']
观看次数
from django.contrib import messages
from django.shortcuts import redirect, render
from . import models
def MedicalSchool(request):
url = request.META.get('HTTP_REFERER') # get last url
if request.method == 'POST':
form = models.MedicalSchoolForm(request.POST) # access to ModelForm
if form.is_valid():
data = models.MedicalSchool() # create a instance of your Model
data.school_name = form.cleaned_data['school_name'] # 'school_name' is the name that we specified in html form input
data.m_pass_out = form.cleaned_data['m_pass_out'] # 'm_pass_out' is the name that we specified in html form input
data.doctor_profile_id = form.cleaned_data['doctor_profile'] # 'doctor_profile' is the name that we specified in html form input
data.save()
return redirect(url)
else:
messages.warning(request, form.errors)
return redirect(url)
context = {'DoctorProfile': models.DoctorProfile.objects.all()}
return render(request, 'MedicalSchool.html', context)
网址
from django.urls import path
from . import views
app_name = 'School'
urlpatterns = [
path('', views.MedicalSchool, name='MedicalSchool'),
...
]
MedicalSchool.html
<form action="{% url 'School:MedicalSchool' %}" method="POST">
{% csrf_token %}
<input type="text" name="school_name" placeholder="school name" required>
<input type="text" name="m_pass_out" placeholder="m pass out" required>
<select name='doctor_profile'>
{% for dr in DoctorProfile %}
<option name="doctor_profile" value="{{ dr.id }}">{{ dr.title}}</option> <!-- Match your code with {{ dr.title}} -->
{% endfor %}
</select>
<button type="submit"> Submit </button>
</form>
让我知道是否存在问题
这篇关于在Django中如何在不使用form.py的情况下从html表添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文