views.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from django.shortcuts import render, redirect
  2. from django.contrib.auth import authenticate, login, logout
  3. from django.http import HttpResponse
  4. from .form import UserLoginForm, UserRegisterForm
  5. from django.contrib.auth.models import User
  6. from django.contrib.auth.decorators import login_required
  7. from .models import Profile
  8. from .form import ProfileForm
  9. def user_login(request):
  10. if request.method == 'POST':
  11. user_login_form = UserLoginForm(request.POST)
  12. if user_login_form.is_valid():
  13. # 清洗出合法的数据
  14. data = user_login_form.cleaned_data
  15. # 检验账号、密码是否正确匹配数据库中的某个用户
  16. # 如果均匹配,则返回这个user对象
  17. user = authenticate(username=data['username'], password=data['password'])
  18. if user:
  19. login(request, user)
  20. return redirect("article:article_list")
  21. else:
  22. return HttpResponse("账号或密码输入不正确,请重新输入")
  23. else:
  24. return HttpResponse("账号或密码输入不合法")
  25. elif request.method == 'GET':
  26. user_login_form = UserLoginForm()
  27. context = {'form': user_login_form}
  28. return render(request, 'userprofile/login.html', context)
  29. else:
  30. return HttpResponse("请使用GET或POST请求数据")
  31. def user_logout(request):
  32. logout(request)
  33. return redirect("article:article_list")
  34. def user_register(request):
  35. if request.method == 'POST':
  36. user_register_form = UserRegisterForm(data=request.POST)
  37. if user_register_form.is_valid():
  38. new_user = user_register_form.save(commit=False)
  39. new_user.set_password(user_register_form.cleaned_data['password'])
  40. new_user.save()
  41. login(request, new_user)
  42. return redirect("article:article_list")
  43. else:
  44. return HttpResponse('注册表单输入有误,请重新输入')
  45. elif request.method == 'GET':
  46. user_register_form = UserRegisterForm()
  47. context = {'form': user_register_form}
  48. return render(request, 'userprofile/register.html', context)
  49. else:
  50. return HttpResponse('请使用GET或POST请求数据')
  51. @login_required(login_url='/user/login/')
  52. def user_delete(request, id):
  53. if request.method == 'POST':
  54. user = User.objects.get(id=id)
  55. if request.user == user:
  56. logout(request)
  57. user.delete()
  58. return redirect('article:article_list')
  59. else:
  60. return HttpResponse('你没有删除该用户的权限')
  61. else:
  62. return HttpResponse('仅接受POST请求')
  63. @login_required(login_url='/user/login/')
  64. def profile_edit(request, id):
  65. user = User.objects.get(id=id)
  66. profile = Profile.objects.get(user_id=id)
  67. if request.method == 'POST':
  68. if request.user != user:
  69. return HttpResponse('你没有权限修改此用户信息')
  70. profile_form = ProfileForm(request.POST, request.FILES)
  71. if profile_form.is_valid():
  72. profile_cd = profile_form.cleaned_data
  73. profile.phone = profile_cd['phone']
  74. profile.bio = profile_cd['bio']
  75. profile.avatar = profile_cd["avatar"]
  76. profile.save()
  77. return redirect('userprofile:edit', id=id)
  78. else:
  79. return HttpResponse('注册表单有误,请重新输入')
  80. elif request.method == 'GET':
  81. profile_form = ProfileForm()
  82. context = {'profile_form': profile_form, 'profile': profile, 'user': user}
  83. return render(request, 'userprofile/edit.html', context)
  84. else:
  85. return HttpResponse('请使用GET或POST方法请求数据')