12311k

Untitled

May 5th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. views.py
  2. ________
  3. from django.shortcuts import render
  4. from django.core.mail import send_mail
  5. from django.shortcuts import redirect
  6.  
  7. from .forms import ExchangeForm
  8. from .models import GENRE_CHOICES
  9.  
  10. # Create your views here.
  11.  
  12.  
  13. def send_msg(
  14. email, name, title, artist, genre, price, comment,
  15. ):
  16. subject = f"Обмен {artist}-{title}"
  17. body = f"""Предложение на обмен диска от {name} ({email})
  18.  
  19. Название: {title}
  20. Исполнитель: {artist}
  21. Жанр: {genre}
  22. Стоимость: {price}
  23. Комментарий: {comment}
  24.  
  25. """
  26. send_mail(
  27. subject, body, email, ["admin@rockenrolla.net",],
  28. )
  29.  
  30.  
  31. def thank_you_view(request):
  32. return render(request, "thankyou.html")
  33.  
  34.  
  35. def index(request):
  36. if request.method == "POST":
  37. form = ExchangeForm(request.POST)
  38. if form.is_valid():
  39. genre = None
  40. for code, name in GENRE_CHOICES:
  41. if code == form.cleaned_data["genre"]:
  42. genre = name
  43. break
  44. send_msg(
  45. form.cleaned_data["email"],
  46. form.cleaned_data["name"],
  47. form.cleaned_data["title"],
  48. form.cleaned_data["artist"],
  49. genre,
  50. form.cleaned_data["price"],
  51. form.cleaned_data["comment"],
  52. )
  53. return redirect('/thank-you/')
  54. return render(request, "index.html", {"form": form})
  55.  
  56. form = ExchangeForm()
  57. return render(request, "index.html", {"form": form})
  58.  
  59. __________
  60. index.html
  61. __________
  62.  
  63. <!DOCTYPE html>
  64. <!-- Based on https://getbootstrap.com/docs/4.4/examples/pricing/ -->
  65. <html lang="ru">
  66. <head>
  67. <meta charset="utf-8" />
  68. <meta
  69. name="viewport"
  70. content="width=device-width, initial-scale=1, shrink-to-fit=no"
  71. />
  72. <title>Уголок рокнрольщика</title>
  73.  
  74. <!-- Bootstrap core CSS -->
  75. <link
  76. rel="stylesheet"
  77. href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
  78. integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
  79. crossorigin="anonymous"
  80. />
  81. </head>
  82.  
  83. <body>
  84. <div class="container">
  85. <h1>Предложение обмена</h1>
  86. <p class="lead">
  87. Предложите мне свой диск на обмен.
  88. </p>
  89.  
  90. <form method="post">
  91. {% csrf_token %}
  92. {{ form.as_p }}
  93. <input type="submit" value="Отправить">
  94. </form>
  95.  
  96. </div>
  97.  
  98. </body>
  99. </html>
Add Comment
Please, Sign In to add comment