如何在django的用户模块的个人资料页面中编辑特定的用户详细信息

时间:2019-07-08 17:29:41

标签: django

我创建了注册表单和登录表单。现在我要编辑和删除注册表单的特定详细信息。我尝试了以下代码,但无法正常工作。它只允许我编辑管理面板用户详细信息

这是我的代码

models.py

from django.db import models
from django.contrib.auth.models import User


class UserProfileInfo(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)


    def __str__(self):
        return self.user.username

forms.py

from django import forms
from .models import UserProfileInfo
from django.contrib.auth.models import User



class UserProfileInfoForm(forms.ModelForm):
    class Meta():
         model = UserProfileInfo
         fields = [ ]

class editProfile(forms.ModelForm):
    class Meta():
        model = User
        fields = ['first_name','last_name','username','email','password']
        help_texts={
            'username': None
        }

views.py

from django.shortcuts import render
from .forms import UserForm,UserProfileInfoForm,editProfile

from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.urls import reverse
from django.contrib.auth.decorators import login_required



def edit_profile(request):
    if request.POST:
        user = User.objects.get(pk=request.user.id)
        user.username=request.POST.get('user')                    
        user.email=request.POST.get('email') 
        user.first_name=request.POST.get('first_name')                    
        user.last_name=request.POST.get('last_name')
        user.password=request.POST.get('password')                    

        user.save()
        return HttpResponseRedirect(reverse('signupapp:index'))    

    user_profile = request.user.get_profile()
    return render_to_response('edit.html',{'profile':user_profile },context_instance=RequestContext(request))

谁能帮我解决这个问题。

0 个答案:

没有答案