第一个问题是,当出现更新表单时,生日字段没有从数据库获取数据。 第二件事是,每当我单击“提交”按钮时,它都不会更新数据库中的值。 第三件事是,当我单击“更新”或“提交”按钮后,它将自动以我不想在其中的插入形式重定向到我(而且我什至不提供链接或渲染到该页面,并且我不了解这里发生的事情,因为它不是它而是它需要显示我的editstudent.html页面而不是insertstudent.html页面) 是的!!!我的更新也无法正常工作...... 请帮助我。......
editstudent.html页面:-
{% extends 'student/index.html' %}
{% block content %}
<div class="col-md-6">
<!-- general form elements -->
<div class="card card-primary">
<div class="card-header">
<h3 class="card-title">Update Student Here</h3>
</div>
<!-- /.card-header -->
<!-- form start -->
<form action="{% url 'studentinsert' %}" role="form" method="POST">
{% csrf_token %}
<div class="card-body">
<div class="form-group">
<label for="exampleInputEmail1">Student ID</label>
<input type="text" class="form-control" name="id" placeholder="Enter Student ID" value="{{ student.sid }}">
</div>
<div class="form-group">
<label for="exampleInputPassword1">First Name</label>
<input type="text" class="form-control" name="firstname" placeholder="Enter First Name" value="{{ student.first_Name }}">
</div>
<div class="form-group">
<label for="exampleInputFile">Last Name</label>
<div class="input-group">
<div class="custom-file">
<input type="text" class="form-control" name="lastname" placeholder="Enter Last Name" value="{{ student.last_name }}">
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile">Major</label>
<div class="input-group">
<div class="custom-file">
<input type="text" class="form-control" name="major" placeholder="Enter Major" value="{{ student.major }}">
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile">Phone Number</label>
<div class="input-group">
<div class="custom-file">
<input type="text" class="form-control" name="phonenumber" placeholder="Enter the Phone Number" value="{{ student.phone }}">
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile">GPA</label>
<div class="input-group">
<div class="custom-file">
<input type="text" class="form-control" name="gpa" placeholder="Enter GPA" value="{{ student.gpa }}">
</div>
</div>
</div>
<div class="form-group">
<label for="exampleInputFile">Date-of-Birth</label>
<div class="input-group">
<div class="custom-file">
<select name="DOBMonth" value="{{ student.date_of_birth }}">
<option>- Month -</option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
</select>
<select name="DOBDay" value="{{ student.date_of_birth }}>
<option>- Day -</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<select name="DOBYear" value="{{ student.date_of_birth }}>
<option>- Year -</option>
<option value="2020">2020</option>
<option value="2019">2019</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
</select>
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
<!-- /.card -->
</div>
{% endblock %}
我的Views.py页面:-
def upstudent(request,pk):
obj=Student.objects.get(id=pk)
if request.method=="POST":
iid=request.POST['id']
firstname=request.POST['firstname']
lastname=request.POST['lastname']
major=request.POST['major']
phonenumber=request.POST['phonenumber']
gpa=request.POST['gpa']
birth=request.POST['DOBDay']
month=request.POST['DOBMonth']
year=request.POST['DOBYear']
d = Date()
d.day = birth
d.month = month
d.year = year
d.save()
s=Student(Student.objects.get(id=obj))
s.date_of_birth = d
s.sid=iid
s.first_Name=firstname
s.last_name=lastname
s.major=major
s.phone=phonenumber
s.gpa=gpa
s.save()
return HttpResponse("Data is updated")
else:
student = Student.objects.get(id=pk)
return render(request,"student/editstudent.html",{"student":student})
我的Models.py页面:-
class Date(models.Model):
month=models.CharField(max_length=200)
day=models.CharField(max_length=200)
year=models.CharField(max_length=200)
class Student(models.Model):
sid=models.CharField(max_length=200)
first_Name=models.CharField(max_length=200)
last_name=models.CharField(max_length=200)
major=models.CharField(max_length=200)
phone=models.CharField(max_length=200)
gpa=models.CharField(max_length=200)
date_of_birth = models.ForeignKey(Date,on_delete=models.CASCADE)
MY Urls.py页面:-
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path("index",views.index,name="index"),
path("studentinsert",views.studentinsert,name='studentinsert'),
path("studentdata",views.studentdata,name="studentdata"),
path("upstudent/<int:pk>",views.upstudent,name="upstudent")
]
答案 0 :(得分:1)
您的代码中存在很多问题:
为什么您的date_of_birth
是通过ForeignKey
而不是OneToOneField
链接的? Student
可以有多个出生日期吗?如果将其交换为一对一关系,则可以进行以下更改。
第一个问题是,当出现更新表单时,生日字段没有从数据库中获取数据。
发生这种情况是因为没有value
标签的select
属性-您必须将selected
属性放在option
标签上。将您的代码更改为:
<select name="DOBMonth">
<option>- Month -</option>
<option value="January"{% if student.date_of_birth.month == 'January' %} selected="selected"{% endif %}>January</option>
<option value="Febuary"{% if student.date_of_birth.month == 'Febuary' %} selected="selected"{% endif %}>Febuary</option>
<option value="March"{% if student.date_of_birth.month == 'March' %} selected="selected"{% endif %}>March</option>
<option value="April"{% if student.date_of_birth.month == 'April' %} selected="selected"{% endif %}>April</option>
<option value="May"{% if student.date_of_birth.month == 'May' %} selected="selected"{% endif %}>May</option>
<option value="June"{% if student.date_of_birth.month == 'June' %} selected="selected"{% endif %}>June</option>
</select>
每年的日子都一样。
它重定向您,因为form
标签action
属性指向错误的视图。当前为action="{% url 'studentinsert' %}"
,应为action="{% url 'upstudent' pk=student.pk %}"
正如@Mehran所说,您不应该重新提取Student
。只需这样做:
obj.date_of_birth = d
obj.sid=iid
obj.first_Name=firstname
obj.last_name=lastname
obj.major=major
obj.phone=phonenumber
obj.gpa=gpa
obj.save()
学习django forms-这将大大简化您的代码。
答案 1 :(得分:0)
好的,从最近的评论中,我想我了解发生了什么。在这里,您正在创建一个新学生或显示已经创建的学生数据。我将更新模型并查看。
模型
class Student(models.Model):
sid=models.CharField(max_length=200)
first_Name=models.CharField(max_length=200)
last_name=models.CharField(max_length=200)
major=models.CharField(max_length=200)
phone=models.CharField(max_length=200)
gpa=models.CharField(max_length=200)
date_of_birth = models.DateField(blank=True, null=True)
Viiews
def upstudent(request,pk):
student=Student.objects.filter(id=pk).first()
if request.method=="POST":
iid=request.POST['id']
firstname=request.POST['firstname']
lastname=request.POST['lastname']
major=request.POST['major']
phonenumber=request.POST['phonenumber']
gpa=request.POST['gpa']
datefield = request.post['datefield']
# depending on your datefield input, which will be a string
# this needs to be converted to a date object
# use .strptime() function for this
if not student:
student = Student() # creating a new student object
student.sid=iid
student.first_Name=firstname
student.last_name=lastname
student.major=major
student.phone=phonenumber
student.gpa=gpa
student.date_of_birth = datefield
student.save()
return HttpResponse("Data is updated")
else:
return render(request,"student/editstudent.html",{"student":student})
编辑,日期格式为<input type="date" value="2010-12-16;">
答案 2 :(得分:0)
您在表单操作中使用了错误的网址名称
使用<form action="{% url 'upstudent' %}" role="form" method="POST">
代替<form action="{% url 'studentinsert' %}" role="form" method="POST">