创建views.py到管理页面

时间:2017-02-13 17:52:54

标签: django python-3.x

我需要制作一个可以节省大量学生信息的页面,例如当我们想要添加新用户/学生时,我们应该输入一些必填字段。实际上我可以添加并保存姓名,姓氏,NO等等......但是我不能像这张照片为一个学生节省很多课程。 my page

我想要的是我的项目的视图。我尝试了很多算法,但我无法保存到数据库/管理页面。 我的admin.py

from django.contrib import admin
from .models import Students

class StudentsAdminInline(admin.TabularInline):
      model =Students

class StudentsAdmin(admin.ModelAdmin):
    list_display =   ["__str__","o_no","f_name","l_name","University","date","timestamp","update"]
    inlines = [StudentsAdminInline]
    admin.site.register(Students)

我的model.py

from django.db import models

class Students (models.Model):
    email=models.EmailField()
    f_name=models.CharField(default="",max_length=50)
    l_name = models.CharField(default="",max_length=50,blank=False)
    o_no=models.CharField(default="",null=False,max_length=50)
    University=models.CharField(default="",max_length=50)
    date=models.DateField(editable=True,null=True)
    timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)
    update=models.DateTimeField(auto_now_add=False,auto_now=True)


    def __str__(self):
        return '{}'.format(self.id)

class Student (models.Model):
    student=models.ForeignKey(Students,related_name='items')
    Lessons=models.CharField(default="",max_length=50,blank=False)
    Code=models.DecimalField(max_digits=2, decimal_places=1,null=True)
    Success=models.CharField(default="",max_length=50,blank=False)
    def __str__(self):
        return '{}'.format(self.id)

my forms.py

class students_form_admin(forms.ModelForm):
    class Meta:
        model=Students
        fields=["__str__","o_no","f_name","l_name","University","date","timestamp","update"]


class student_form_admin(forms.ModelForm):
    class Meta:
        model=Student
        fields=["Lessons","Code","Success"]

2 个答案:

答案 0 :(得分:0)

您需要使用Django Formsets。在Django Formsets.

处获取战利品

或者,如果您只想使用django admin,则应将代码更改为:

admin.py:

from django.contrib import admin
from .models import Student, Lesson


class StudentAdminInline(admin.TabularInline):
      model =Lesson

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
    list_display =   ["__str__","o_no","f_name","l_name","University","date","timestamp","update"]
    inlines = [StudentAdminInline]

models.py:

from django.db import models

class Student (models.Model):
    email=models.EmailField()
    f_name=models.CharField(default="",max_length=50)
    l_name = models.CharField(default="",max_length=50,blank=False)
    o_no=models.CharField(default="",null=False,max_length=50)
    University=models.CharField(default="",max_length=50)
    date=models.DateField(editable=True,null=True)
    timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)
    update=models.DateTimeField(auto_now_add=False,auto_now=True)


    def __str__(self):
        return '{}'.format(self.id)

class Lesson (models.Model):
    student=models.ForeignKey(Student,related_name='items')
    name=models.CharField(default="",max_length=50,blank=False)
    Code=models.DecimalField(max_digits=2, decimal_places=1,null=True)
    Success=models.CharField(default="",max_length=50,blank=False)
    def __str__(self):
        return '{}'.format(self.id)

forms.py:

from .models import Student, Lesson

class students_form_admin(forms.ModelForm):
    class Meta:
        model=Student
        fields=["__str__","o_no","f_name","l_name","University","date","timestamp","update"]


class student_form_admin(forms.ModelForm):
    class Meta:
        model=Lesson
        fields=["Lessons","Code","Success"]

答案 1 :(得分:0)

好的,所以我建议您阅读如何创建视图。 Django网站有很多非常有用的信息。 Here是用于创建视图的文档。

至于保存到数据库。 Here是有关在项目中设置和使用数据库的文档。 This link将向您展示如何使用模型和对象来保存视图中的数据。

Django网站提供各种可以帮助您的信息。