Django从另一种形式

时间:2017-03-18 14:29:32

标签: python django

我是Django的新手,想知道如何在教师使用下面显示的LoginFormView登录时保存教师的电子邮件。在教师登录后,他可以使用CreateClassForm创建一个类。

当教师创建新类时,我在尝试将ClassTeacherTeaches模型存储到CreateClassForm下的数据库时遇到问题。如何从LoginFormView检索教师的电子邮件并存储教师的电子邮件,并在ClassTeacherTeaches模型数据库中获取自动递增的课程ID(这是在教师创建新课程后创建的)?

models.py

class User(AbstractBaseUser):
    email = models.EmailField(max_length=100, primary_key=True)
    password = models.CharField(max_length=100, null=False)
    full_name = models.CharField(max_length=100, default="", null=False)
    USERNAME_FIELD = 'email'

    def get_full_name(self):
        return self.full_name

    def set_full_name(self, fname):
        self.full_name = fname

    def get_short_name(self):
        pass

    def get_full_name(self):
        pass

class Class(models.Model):
    classid = models.AutoField(primary_key=True)
    class_name = models.CharField(max_length=100, null=False)


class ClassTeacherTeaches(models.Model):
    class Meta:
        unique_together = (('classid', 'teacher_email'),)

    classid = models.ForeignKey(Class)
    teacher_email = models.ForeignKey(User)

form.py

 class CreateClassForm(forms.Form):
        class_name = forms.CharField(widget=forms.widgets.TextInput(attrs={'placeholder': 'Module Code'}), label='')

        class Meta:
            model = Class
            fields = ['class_name']

        def save(self, commit=True):
            data = self.cleaned_data
            module = Class(class_name=data['class_name'])

            if commit:
                module.save()
            return module

  class LoginFormView(FormView):
            form_class = LoginForm
            template_name = 'App/index.html'
            success_url = '/'

            def post(self, request, *args, **kwargs):
                login_form = self.form_class(request.POST)
                register_form = RegistrationForm()
                if login_form.is_valid():
                    auth = EmailAuthBackend()
                    user = auth.authenticate(email=request.POST['email'], password=request.POST['password'])
                    if user is not None:

                        django_login(request, user)
                        return HttpResponseRedirect("../instructormodule")

                else:
                    return self.render_to_response(
                        self.get_context_data(
                            login_form=login_form,
                            register_form=register_form
                        )
                    )  

2 个答案:

答案 0 :(得分:2)

您不会从登录表单中检索教师的电子邮件"。老师已登录;因此,您从登录的uesr中检索电子邮件。

class CreateClassForm(forms.Form):
    ...
    def save(self, commit=True):
        data = self.cleaned_data
        module = Class(class_name=data['class_name'])
        module.teacher_email = self.request.user

        if commit:
            module.save()
        return module

注意,调用该字段teacher_email是没有意义的;它指向整个用户模型,因此应该只调用teacher

答案 1 :(得分:1)

我同意丹尼尔的回答,但我想在这里给出更好的指导。通过ForeignKey将您的Class模型链接到您的用户。这样您就可以将类与用户相关联,因此您将获得指向创建该类的用户的指针。

请阅读关于此的django文档:

https://docs.djangoproject.com/en/1.10/topics/db/examples/many_to_one/