views.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from django.shortcuts import render,HttpResponseRedirect
  2. from django.contrib.auth import authenticate,login,logout
  3. from .forms import SignUpForm,LoginForm,PostForm
  4. from django.contrib import messages
  5. from .models import Post
  6. from django.contrib.auth.models import Group
  7. # Create your views here.
  8. #Home
  9. def home(request):
  10. posts = Post.objects.all()
  11. return render(request,'myapp/home.html',{'posts':posts})
  12. #About
  13. def about(request):
  14. return render(request,'myapp/about.html')
  15. #Contact
  16. def contact(request):
  17. return render(request,'myapp/contact.html')
  18. #Signup
  19. def signup(request):
  20. if request.method == "POST":
  21. form = SignUpForm(request.POST)
  22. if form.is_valid():
  23. messages.success(request,'Congratulations!!! You have become an Author')
  24. user=form.save()
  25. group =Group.objects.get(name='Author')
  26. print(group)
  27. user.groups.add(group)
  28. else:
  29. form = SignUpForm()
  30. return render(request,'myapp/signup.html',{'form':form})
  31. #Dashboard
  32. def dashboard(request):
  33. if request.user.is_authenticated:
  34. posts = Post.objects.all()
  35. user = request.user
  36. full_name = user.get_full_name()
  37. gps = user.groups.all()
  38. return render(request, 'myapp/dashboard.html', {'posts':posts,'full_name':full_name,'groups':gps})
  39. else:
  40. return HttpResponseRedirect('/login/')
  41. #Login
  42. def user_login(request):
  43. if not request.user.is_authenticated:
  44. if request.method == "POST":
  45. form = LoginForm(request=request,data=request.POST)
  46. if form.is_valid():
  47. uname = form.cleaned_data['username']
  48. upass = form.cleaned_data['password']
  49. user = authenticate(username=uname,password=upass)
  50. if user is not None:
  51. login(request,user)
  52. messages.success(request,'Logged in Successfully!!!')
  53. return HttpResponseRedirect('/dashboard/')
  54. else:
  55. form = LoginForm()
  56. return render(request,'myapp/login.html',{'form':form})
  57. else:
  58. return HttpResponseRedirect('/login/')
  59. #Addpost
  60. def add_post(request):
  61. if request.user.is_authenticated:
  62. if request.method == 'POST':
  63. form = PostForm(request.POST)
  64. if form.is_valid():
  65. title = form.cleaned_data['title']
  66. desc = form.cleaned_data['desc']
  67. pst = Post(title=title, desc=desc)
  68. pst.save()
  69. form = PostForm()
  70. else:
  71. form = PostForm()
  72. return render(request, 'myapp/addpost.html', {'form':form})
  73. else:
  74. return HttpResponseRedirect('/login/')
  75. # Update/Edit Post
  76. def update_post(request, id):
  77. if request.user.is_authenticated:
  78. if request.method == 'POST':
  79. pi = Post.objects.get(pk=id)
  80. form = PostForm(request.POST, instance=pi)
  81. if form.is_valid():
  82. form.save()
  83. else:
  84. pi = Post.objects.get(pk=id)
  85. form = PostForm(instance=pi)
  86. return render(request, 'myapp/update.html', {'form':form})
  87. else:
  88. return HttpResponseRedirect('/login/')
  89. # Delete Post
  90. def delete_post(request, id):
  91. if request.user.is_authenticated:
  92. if request.method == 'POST':
  93. pi = Post.objects.get(pk=id)
  94. pi.delete()
  95. return HttpResponseRedirect('/dashboard/')
  96. else:
  97. return HttpResponseRedirect('/login/')
  98. #Logout
  99. def user_logout(request):
  100. logout(request)
  101. return HttpResponseRedirect('/')