Django значения формы не вставлены в таблицы базы данных sqlite3 - PullRequest
0 голосов
/ 04 марта 2020

Я новичок ie в веб-разработке с использованием Django. Я создал 3 модели и соответственно их формы. Даже команды makemigrations и migrate не сообщали о каких-либо проблемах, и таблицы также создаются в файле БД SQLite. Но все же значения не вставляются в файл БД. Получен пустой QuerySet, т.е. <QuerySet []>. Может кто-нибудь, пожалуйста, дайте мне знать, почему значения не вставляются? Заранее спасибо.

Фрагмент программы: -

model.py

class User(models.Model):

    name = models.CharField(max_length=100, default='ABC')
    password = models.CharField(max_length=50, default='ABC')

    def __str__(self):
        print("self.name:", self.name)
        return self.name

class Faculty_Info(models.Model):

    fid = models.IntegerField(default='123')
    fname = models.CharField(max_length=50, default="ABC")
    femail = models.EmailField(max_length=50, default='abc@gmail.com')
    fcontact = models.IntegerField(default='123')
    faddress = models.TextField(max_length=50, default='ABC')
    fsalary = models.IntegerField(default='123')

    def __str__(self):
        return self.fname

class Student_Info(models.Model):

    sid = models.IntegerField(default='123')
    sname = models.CharField(max_length=50, default='ABC')
    semail = models.EmailField(max_length=50, default='abc@gmail.com')
    scontact = models.IntegerField(default='123')
    saddress = models.TextField(max_length=50, default='ABC')

    def __str__(self):
        return self.sname

просмотров. py

def faculty_info_section(request):
    faculties = Faculty_Info.objects.all()
    print("faculties", faculties)
    return render(request, "faculty_info_section.html", {'faculties': faculties})


def add_faculty(request):
    if request.method == "POST":
        faculty_form = Faculty_Info_Form(request.POST)
        print("faculty_form", faculty_form)
        if faculty_form.is_valid():
            try:
                faculty_form.save()
                print("faculty_form:", faculty_form)
                return redirect('/faculty_info_section')
            except:
                pass
    else:
        faculty_form = Faculty_Info_Form()
    print("faculty_form_2", faculty_form)
    return render(request, 'add_faculty.html', {'faculty_form': faculty_form})

faculty_info_section. html

    {% for faculty in faculties %}
    <tr>
        <td>{{ faculty.fid }}</td>
        <td>{{ faculty.fname }}</td>
        <td>{{ faculty.femail }}</td>
        <td>{{ faculty.fcontact }}</td>
        <td>{{ faculty.faddress }}</td>
        <td>{{ faculty.fsalary }}</td>
        <td>
            <a href="/edit/{{ faculty.id }}"><span class="glyphicon glyphicon-pencil">Edit</span></a>
            <a href="/delete/{{ faculty.id }}">Delete</a>
        </td>
    </tr>
{% endfor %}

forms.py

from django import forms
from django.forms import Textarea

from School.models import User, Faculty_Info, Student_Info


class UserLoginForm(forms.ModelForm):
    class Meta:
        model = User
        fields = "__all__"


class Faculty_Info_Form(forms.ModelForm):
    class Meta:
        model = Faculty_Info
        widgets = {
            'fid': Textarea(attrs={'cols': 30, 'rows': 2}),
            'fname': Textarea(attrs={'cols': 30, 'rows': 2}),
            'femail': Textarea(attrs={'cols': 30, 'rows': 2}),
            'fcontact': Textarea(attrs={'cols': 30, 'rows': 2}),
            'faddress': Textarea(attrs={'cols': 30, 'rows': 2}),
            'fsalary': Textarea(attrs={'cols': 30, 'rows': 2}),
        }
        fields = "__all__"


class Student_Info_Form(forms.ModelForm):
    class Meta:
        model = Student_Info
        widgets = {
            'sid': Textarea(attrs={'cols': 30, 'rows': 2}),
            'sname': Textarea(attrs={'cols': 30, 'rows': 2}),
            'semail': Textarea(attrs={'cols': 30, 'rows': 2}),
            'scontact': Textarea(attrs={'cols': 30, 'rows': 2}),
            'saddress': Textarea(attrs={'cols': 30, 'rows': 2}),
        }
        fields = "__all__"

Пожалуйста, помогите, заранее спасибо.

...