django - custom_user:如何制作和EditProfile视图?

时间:2016-10-06 15:18:43

标签: django django-views django-validation

我已经安装了django-registration-redux和custom_user。我有一个Username模型,其中包含fullname,dob和photo作为额外字段。

我已将UserProfile与用户注册相关联,以便将其他字段保存到UserProfile表。

我的问题是......

  1. 如何在注册时验证UserProfile模型中的字段,以便我可以阻止将用户添加到emailuser表。比如,如果给出的dob无效或不在允许的范围内,则停止将用户添加到emailuser表。
  2. 如何创建允许经过身份验证的用户编辑其个人资料的EditProfile系统(/ user / edit-id从会话中获取)。
  3. 以下是我的 settings.py 文件

    INSTALLED_APPS = [
        'django.contrib.admin',
        'custom_user',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        #------------
        'registration',
        'userprofile',    
    ]
    
    AUTH_USER_MODEL = 'custom_user.EmailUser'
    

    models.py

    class UserProfile(models.Model):
        user=models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
            primary_key=True,)
        fullname=models.CharField(max_length=70, blank=False)
        dob=models.DateField(blank=False)
        photo=models.ImageField(upload_to='profile_images', blank=False)
        def __str__(self):
            return self.user.email   
    
    
    def user_registered_callback(sender, user, request, **kwargs):
        profile = UserProfile(user = user)
        profile.fullname =request.POST["fullname"]
        profile.dob ="%s-%s-%s" % (request.POST["dob_year"],request.POST["dob_month"],request.POST["dob_day"])
        if 'photo' in request.FILES:
            profile.photo = request.FILES['photo']
    
        profile.save()
    
    user_registered.connect(user_registered_callback)
    

    forms.py

    class UserProfileForm(RegistrationForm):
        fullname=forms.CharField(required=True,label="Full name",  min_length=3, max_length=75)
        dob=forms.DateField(required=True,label="Date of birth", 
            widget=forms.SelectDateWidget(years=range(now.year-settings.AGE_UPPER,now.year-settings.AGE_LOWER)))
        photo=forms.ImageField(required=False)
    

    views.py

    def profile(request):
        if not request.user.is_authenticated():
            return HttpResponseRedirect('/user/login')
        else:
             return render(request, 'userprofile/profile.html', {"user":request.user})
    
    class EditProfile(UpdateView):
        template_name = 'userprofile/edit_profile.html'
    
        fields = ['fullname','dob','photo']
    

    urls.py

    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'user/register/$', 
            RegistrationView.as_view(form_class = UserProfileForm), 
            name = 'registration_register'),
    
        url(r'^user/', include('registration.backends.default.urls')),
        url(r'^user/', include('userprofile.urls')),
    

    userprofile.urls.py

    urlpatterns = [
        url(r'^profile/$', views.profile, name='profile'),
        url(r'^profile/edit/$', views.EditProfile.as_view(), name='edit_profile'),
     ]
    

    现在我正在使 EditProfile缺少QuerySet。定义EditProfile.model,EditProfile.queryset或覆盖EditProfile.get_queryset()。

    这是正确的方法吗?任何帮助将不胜感激。

    由于

0 个答案:

没有答案