无法在Django中的数据库中添加表单数据

时间:2017-11-14 07:16:26

标签: django django-formwizard

我试图将数据从我的表单保存到我的数据库,但不知何故它没有发生。我已经在我的管理员使用的一个应用程序中创建了表单。我是Django的新手,非常感谢任何帮助。以下是相关代码:

models.py

from django import forms
from tcp.models import Login, Students, Facultys , Admins

class StudentForm(forms.ModelForm):
    username = forms.CharField(max_length=128, help_text="Please enter the name.")
    password = forms.CharField(widget = forms.PasswordInput(), help_text = 'please enter the password')
    usn= forms.CharField(max_length=10 , help_text = "Please enter the USN")

    class Meta:
        model = Students
        fields = ['username' , 'password' , 'usn']


class FacultyForm(forms.ModelForm):
    username = forms.CharField(max_length=10 , help_text = "Please enter the name")
    password = forms.CharField(widget = forms.PasswordInput() , help_text = 'Please enter the password')
    faculty_id = forms.CharField(max_length=10, help_text= "Please enter your id")

    class Meta:
        model = Facultys
        fields= ['username' , 'password' , 'faculty_id']


class AdminForm(forms.ModelForm):
    username = forms.CharField(max_length=128, help_text="Please enter the name.")
    password = forms.CharField(widget = forms.PasswordInput(), help_text = 'please enter the password')
    admin_id= forms.CharField(max_length=10 , help_text = "Please enter the id")


    class Meta:
        model = Admins
        fields = ['username' , 'password' , 'admin_id']

forms.py

from __future__ import unicode_literals

from django.shortcuts import render
from tcp.forms import LoginForm,StudentForm,AdminForm,FacultyForm
from django.contrib.auth.hashers import make_password


def indexx(request):
    return render(request, 'tcp/firstlook.html')


def student_signup(request):
    form = StudentForm()
    if request.method =='POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            form.save(commit = True)

            return index(request)
        else:
            print form.errors

    return render(request , 'tcp/student_login.html', {'form':form})

def faculty(request):
    form =FacultyForm()
    if request.method == 'POST':
        form =FacultyForm(request.POST)
        if form.is_valid():
            form.save(commit = True)
            return indexz(request)
        else:
            print form.errors
    return render(request , 'tcp/faculty_login.html', {'form': form})

def admin(request):
    form=AdminForm()
    if request.method=='POST':
        form=AdminForm(request.POST)
        if form.is_valid():
            form.save(commit =True)
            return indexzz(request)
        else:
            print form.errors
    return render(request , 'tcp/admin.html' , {'form' :form})

views.py

from __future__ import unicode_literals

from django.contrib import admin

from .models import Placement_Department,Spc, Login,Students, Facultys, Admins


admin.site.register(Login)
admin.site.register(Students)
admin.site.register(Facultys)
admin.site.register(Admins)

这是我的admin.py文件,我已将模型注册到网站管理

admin.py

{{1}}

但是我的学生表单正在将数据保存在数据库中。

0 个答案:

没有答案