Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. from django.contrib.auth import authenticate, login, get_user_model
  2. from django.http import HttpResponse
  3. from django.shortcuts import render, redirect
  4.  
  5. from .forms import ContactForm, LoginForm, RegisterForm
  6.  
  7. def home_page(request):
  8. context = {
  9. "title":"Hello World!",
  10. "content":" Welcome to the homepage.",
  11.  
  12. }
  13. if request.user.is_authenticated:
  14. context["premium_content"] = "YEAHHHHHH"
  15. return render(request, "home_page.html", context)
  16.  
  17.  
  18.  
  19.  
  20.  
  21. def about_page(request):
  22. context = {
  23. "title":"About Page",
  24. "content":" Welcome to about page."
  25. }
  26. return render(request, "home_page.html", context)
  27.  
  28. def contact_page(request):
  29. contact_form = ContactForm(request.POST or None)
  30. context = {
  31. "title":"Contact",
  32. "content":" Welcome to the contact page.",
  33. "form": contact_form
  34. }
  35. if contact_form.is_valid():
  36. print(contact_form.cleaned_data)
  37. #if request.method == "POST":
  38. #print(request.POST)
  39. #print(request.POST.get('fullname'))
  40. #print(request.POST.get('email'))
  41. #print(request.POST.get('content'))
  42.  
  43. return render(request, "contact/view.html", context)
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. def login_page(request):
  51. form = LoginForm(request.POST or None)
  52. context = {
  53. "form": form
  54. }
  55. print("User logged in")
  56. #print(request.user.is_authenticated())
  57. if form.is_valid():
  58. print(form.cleaned_data)
  59. username = (form.cleaned_data).get("username")
  60. password = (form.cleaned_data).get("password")
  61. user = authenticate(username='john', password='secret')
  62. #print(user)
  63. print(request.user.is_authenticated())
  64. if user is not None:
  65. #print(request.user.is_authenticated())
  66. login(request, user)
  67. #login(request, username=username, password=password)
  68. #context['form'] = LoginForm()
  69. return redirect("/")
  70. # A backend authenticated the credentials
  71. else:
  72. print("Error")
  73. return render(request, "auth/login.html", context)
  74.  
  75.  
  76.  
  77.  
  78.  
  79. User = get_user_model()
  80. def register_page(request):
  81. form = RegisterForm(request.POST or None)
  82. context = {
  83. "form": form
  84. }
  85. if form.is_valid():
  86. print(form.cleaned_data)
  87. username = form.cleaned_data.get("username")
  88. email = form.cleaned_data.get("email")
  89. password = form.cleaned_data.get("password")
  90. new_user = User.objects.create_user(username, email, password)
  91. print(new_user)
  92. return render(request, "auth/login.html", context)
  93.  
  94.  
  95.  
  96. def home_page_old(request):
  97.  
  98.  
  99. html_= """
  100. <!doctype html>
  101. <html lang="en">
  102. <head>
  103. <!-- Required meta tags -->
  104. <meta charset="utf-8">
  105. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  106.  
  107. <!-- Bootstrap CSS -->
  108. <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  109.  
  110. <title>Hello, world</title>
  111. </head>
  112. <body>
  113. <div class='text-center'>
  114. <h1>Hello, world!</h1>
  115. </div>
  116. <!-- Optional JavaScript -->
  117. <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  118. <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  119. <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  120. <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  121. </body>
  122. </html>
  123.  
  124. """
  125. return HttpResponse(html_)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement