无需使用Django中的管理面板即可编辑和删除特定用户

时间:2019-07-10 07:12:38

标签: django

我已经使用django用户模型创建了模型。我可以对创建的表单执行编辑功能。现在,我想使用DELETE按钮删除特定的用户详细信息。这是我的代码

forms.py

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

views.py

def index(request):
    form = GithubInfo(request.POST)
    name=''
    if form.is_valid():
        name = form.cleaned_data.get("name") 
        form.save()
    context = {'form':form , 'name':name}
    return render(request,'signupapp/index.html',context)

def edit_profile(request):
    user = request.user
    form = EditProfileForm(request.POST or None, initial={'first_name':user.first_name, 'last_name':user.last_name, 'username':user.username, 'email':user.email, 'password':user.password})
    if request.method == 'POST':
        if form.is_valid():
            user.first_name = request.POST['first_name']
            user.last_name = request.POST['last_name']
            user.username = request.POST['username']
            user.email = request.POST['email']
            user.password = request.POST['password']
            user.save()
            return HttpResponseRedirect(reverse('signupapp:index'))
    context = {"form": form}
    return render(request, "signupapp/edit_profile.html", context)



def delete_profile(request, username):
    profile = UserProfileInfo.objects.get(username=username)
    if request.method == 'POST':
        profile.delete()
        return HttpResponseRedirect(reverse('signupapp:index'))
    return render(request, "signupapp/delete_profile.html", {'profile': profile})

edit_profile.html

<form method="POST" action="" class="" >
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Save</button>
</form>

delete_profile.html

 <form action="" method ="POST">
    {% csrf_token %}
    <p>Are you sure to delete "{{UserProfileInfo.username}}"?</p>
    <input type="submit" value="Delete"/>
</form>

urls.py

url(r'^index/$',views.index,name='index'),
url(r'^edit/$', views.edit_profile, name='edit'),
url(r'^delete/<str:username>/$', views.delete_profile, name='delete')

这是我的编辑和删除链接,用于编辑和删除其中的详细信息

index.html

<h1>Welcome!</h1>
{% if user.is_authenticated %}
<h3>Firstname: {{ user.first_name }}</h3>
<h3>Lastname: {{ user.last_name }}</h3>
<h3>Username: {{ user.username }}</h3>
<h3>Email: {{ user.email }}</h3>
{% else %}
<h2>Register or Login if you'd like to</h2>
{% endif %}
<a href="{% url 'signupapp:edit' %}"><input type="button" value="Edit"/></a>
<a href="{% url 'signupapp:delete' %}"><input type="button" value="Delete"/></a>

当我在服务器上运行此代码时,出现此错误

TypeError at /delete/
delete_profile() missing 1 required positional argument: 'username'

任何人都可以帮助我克服此错误。 预先感谢。

1 个答案:

答案 0 :(得分:1)

这样更改您的视图:

 def edit_profile(request):
        user = request.user
        if request.method == 'POST':
           form = EditProfileForm(request.POST or None,instance= user)

            if form.is_valid():
               user = form.save()

               user.save()
               return HttpResponseRedirect(reverse('signupapp:index'))
       else:
          form = EditProfileForm()
       return render(request, "signupapp/edit_profile.html", {'form':form,'user':user})

然后在模板中传递请求的用户值,如下所示:

<form method="POST" action="" class="" >
    {% csrf_token %}
    <input type='text' name = 'first_name' value={{user.first_name}}
  <input type='text' name = 'lastt_name' value={{user.last_name}}
 .............................
    <button type="submit">Save</button>
</form>

对于删除,您可以这样做。

def delete_profile(request, username):
    user = UserProfileInfo.objects.get(username=username)
    if user:
          user.delete()
          messages.success(request,'user with username {} deleted'.format(user.username))
          return redirect('redirect where you want')
    else:
        messages.error(request,'no user found')
        return redirect('redirect where you want')