我正在开发一个django项目,我需要为学生通行证应用程序收集数据,view.py如下
def BusPassForm(request):
school = admindb.SchoolDetail.objects.all()
place = Place.objects.all()
subtime = SubTime.objects.all()
if request.method == 'POST':
name = request.POST.get("name")
age = request.POST.get("age")
dob = request.POST.get("dob")
mobile = request.POST.get("mobile")
adhaar_no = request.POST.get("adhaar_no")
address = request.POST.get("address")
school_name = request.POST.get("school_name")
start_place = request.POST.get("start_place")
end_place = request.POST.get("end_place")
time_periode = request.POST.get("time_periode")
profileimage = request.POST.get("profileimage")
idimage = request.POST.get("idimage")
adhaar_image = request.POST.get("adhaar_image")
try:
buspassform = PassForm(name=name, age=age, dob=dob, mobile=mobile, adhaar_no=adhaar_no, address=address, school_name=school_name, start_place=start_place, end_place=end_place, time_periode=time_periode, profileimage=profileimage, idimage=idimage, adhaar_image=adhaar_image)
buspassform.save()
messages.success(request, 'application submitted')
return redirect('dash')
except:
messages.error(request, 'Error in submitting your appliction')
return redirect('buspassform')
print(form.error)
return render(request, 'buspassform.html', {'school':school, 'place':place, 'subtime':subtime})
models.py,以获取以下相同信息
class PassForm(models.Model):
name= models.CharField(max_length=100, default=None)
time_periode = models.ForeignKey(SubTime, on_delete = models.CASCADE, default=True)
school_name = models.ForeignKey(admindb.SchoolDetail, on_delete = models.CASCADE, default=True)
start_place = models.ForeignKey(Place, null=True, blank=True, on_delete =models.CASCADE, related_name="pass_start")
end_place = models.ForeignKey(Place, null=True, blank = True, on_delete = models.CASCADE, related_name = 'pass_end')
age = models.IntegerField(blank=True, null=True, default=True)
dob = models.DateField(default=datetime.date.today)
address = models.TextField(max_length=100, blank=True, null=True)
adhaar_no = models.CharField(max_length=200, null=True, blank=True)
mobile = models.CharField(max_length=200, blank=True, default=None)
idimage = models.ImageField(upload_to='static/ksrtcimage/idimage', null=True, default=None)
adhaar_image = models.ImageField(upload_to='static/ksrtcimage/adhaar', null=True, default=None)
profileimage = models.ImageField(upload_to='static/ksrtcimage/profileimage', null=True, default=None)
例如,urls.py
from django.views.generic import TemplateView
from django.urls import path
from . import views
urlpatterns = [
path('', views.homePage, name='home'),
path('login/', views.loginPage, name='login'),
path('logout/', views.logoutUser, name='logout'),
path('register/', views.registerPage, name='register'),
path('dashboard/', views.dashPage, name='dash'),
path('notfound/', views.notfoundPage, name='notfound'),
path('profile/', views.UserProfilePage, name='profile'),
path('test/', views.TestPage, name='test'),
path('buspassform/', views.BusPassForm, name='buspassform'),
path('trainpassform/', views.TrainPassForm, name='trainpassform')
]
我使用html文件作为一个多阶段的表单,使用输入字段的名称读取数据,并传递它。我也尝试使用django的默认表单函数,但它也不工作,请有人立即给予我一些指示,这是我的大学项目,所以任何建议都是有帮助的,我会尝试它
1条答案
按热度按时间xdyibdwo1#
我忘记将MEDIA_URL添加到设置中,这就是为什么数据没有添加到数据库中的原因。另外,在从表单收集图像数据时,我没有添加request.FILES。对于其他用户,我注意到了这一点