从数据库模型中向登录用户显示他们自己的信息

时间:2017-11-23 11:45:52

标签: python mysql django django-models django-forms

我对Django和网络开发相对较新。基本上,我正在尝试建立一个包含两类用户的网站:客户和供应商。客户和供应商都拥有使用Django内置的“来自django.contrib.auth import login -forms.Form”创建的用户帐户(包含用户名,电子邮件和密码),并存储在mySQL数据库的“auth_user”表中。

但是,供应商还可以创建更详细的配置文件(名称,生物,图像等),这些配置文件是使用名为“SupplierSignupForm”的“forms.ModelForm”创建的,并存储在“auth_user”的单独表中,称为“home_suppliersignup” ”。

我想要发生的是,当供应商登录时,他们可以点击标题中的“我的个人资料”链接,并将其转到我们数据库中“home_suppliersignup”表中的个人资料中。目前,我知道如何使用会话信息从表'auth_user'中调用登录用户ID,然后使用它来链接到各自的'auth_user'配置文件:

views.py

    user = request.user

模板

    <a href="{% url 'supplier_profile' id=user.id %}">My profile</a>

urls.py

    url(r'^supplier-profile/(?P<id>[0-9]+)', supplier_profile, name="supplier_profile")

但我不知道如何使用它从其他数据库表中提取信息(即home_suppliersignup)。

非常感谢任何帮助!

我目前的代码:

models.py

    from __future__ import unicode_literals
    from django.db import models
    from django.core.urlresolvers import reverse
    from django.contrib.auth.models import User

    class SupplierSignUp(models.Model):

        LOCATION_CHOICES=(
            ("Central London", "Central London"),
            ("North London", "North London"),
            ("East London", "East London"),
            ("South London", "South London"),
            ("West London", "West London"),
        )

        user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
        name = models.CharField(max_length=50)
        bio = models.CharField(max_length=50)
        area = models.CharField(max_length=50, choices=LOCATION_CHOICES, null=True)
        booking_link = models.CharField(max_length=100, null=True)
        timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)

        def __str__(self):
            return self.email

        def get_absolute_url(self):
            return reverse("supplier_profile", kwargs={"id": self.id})

forms.py

    from django import forms
    from .models import SupplierSignUp
    from django.contrib.auth import (
        authenticate,
        get_user_model,
        login,
        logout,
        )
    from django.contrib.auth.models import User, Group

    User = get_user_model()

    class SupplierSignUpForm(forms.ModelForm):
        class Meta:
            model = SupplierSignUp
            fields = ['name', 'bio', 'area', 'booking_link']

    class UserLoginForm(forms.Form):
        username = forms.CharField()
        password = forms.CharField(widget=forms.PasswordInput)

        def clean(self, *args, **kwargs):
            username = self.cleaned_data.get("username")
            password = self.cleaned_data.get("password")

            if username and password:
                user = authenticate(username=username, password=password)
                if not user:
                    raise forms.ValidationError("This user does not exist")

                if not user.check_password(password):
                    raise forms.ValidationError("Incorrect password")

                if not user.is_active:
                    raise forms.ValidationError("This user is no longer active")

            return super(UserLoginForm, self).clean(*args, **kwargs)

    class UserRegisterForm(forms.ModelForm):
        username = forms.CharField()
        email = forms.EmailField(label="Email Address")
        email2 = forms.EmailField(label="Confirm Email", widget=forms.TextInput(attrs={'autocomplete':'false'}))
        password = forms.CharField(widget=forms.PasswordInput)
        class Meta:
            model = User
            fields = [
                'username',
                'email',
                'email2',
                'password',
            ]

        def clean_email2(self):
            email = self.cleaned_data.get('email')
            email2 = self.cleaned_data.get('email2')
            if email != email2:
                raise forms.ValidationError("Emails must match")

            email_qs = User.objects.filter(email=email)
            if email_qs.exists():
                raise forms.ValidationError("This email has already been registered")

            return email

1 个答案:

答案 0 :(得分:0)

假设您使用基于类的通用视图,请尝试以下操作:

[FindsBy(How = How.XPath, Using = "//iframe[contains(@id,'" + VAR + "')]")]
public IWebElement iframe{ get; set; }

您正在从请求中获取登录用户,是否需要在urls.py / template中指定用户ID(除非您希望用户能够查看其他人的个人资料)。