-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
153 lines (119 loc) · 3.54 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import sys
from django.conf import settings
from django.urls import path
from django.http import HttpResponse, HttpResponseRedirect
from django.core.asgi import get_asgi_application
from django.core.wsgi import get_wsgi_application
from django.template import RequestContext, Template
from django import forms
### SETTINGS
settings.configure(
DEBUG=True,
# TODO: set your allowed hosts accordingly. See https://docs.djangoproject.com/en/5.0/ref/settings/#allowed-hosts
# ALLOWED_HOSTS=[],
ROOT_URLCONF=__name__,
SECRET_KEY="this.is.insecure.please.change!", # SECURITY WARNING: keep the secret key used in production secret!
MIDDLEWARE_CLASSES=(
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
),
TEMPLATES=[
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
}
],
)
### FORMS
class NewsletterForm(forms.Form):
email = forms.EmailField(
required=True,
label=False,
widget=forms.EmailInput(attrs={"placeholder": "Email"}),
)
### VIEWS
def home(request):
if request.method == "POST":
form = NewsletterForm(request.POST)
if form.is_valid():
email = form.cleaned_data["email"]
# TODO: store your new signup somewhere
print(f"New signup: {email}")
return HttpResponseRedirect("/success/")
else:
form = NewsletterForm()
context = RequestContext(
request,
{
"content": "Sign up to the newsletter",
"form": form,
},
)
return HttpResponse(MAIN_HTML.render(context))
def success(request):
context = RequestContext(
request,
{
"content": "Thanks for signing up to the newsletter!",
},
)
return HttpResponse(MAIN_HTML.render(context))
### URLS
urlpatterns = [
path("", home),
path("success/", success),
]
### APPLICATION
application = get_wsgi_application()
### NOTE: If you want to run your application with ASGI then change this to
### application = get_asgi_application()
### TEMPLATES
MAIN_HTML = Template("""
<html>
<head>
<title>MyProject</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
*{
color: #525252;
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,body{
display: grid;
height: 100%;
width: 100%;
place-items: center;
}
.content{
max-width: 900px;
text-align: center;
padding: 0 50px;
}
</style>
</head>
<body>
<div class="content">
<h1>django-tiniest</h1>
<h3>The install worked successfully! Congratulations!</h3>
<br/><br/>
<h3>Here a demo form:</h3>
<br/><br/>
{{ content }}
{% if form %}
<form action="." method="post">
{% csrf_token %}
{{ form.non_field_errors }}
{{ form.email.errors }}
{{ form.email }}
<button type="submit">Sign Me Up</button>
</form>
{% endif %}
</div>
</body>
</html>
""")
if __name__ == "__main__":
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)