booksing write done!
This commit is contained in:
parent
3b9549a9f0
commit
e349050758
|
@ -0,0 +1,15 @@
|
|||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import gettext_lazy as txt
|
||||
|
||||
class SignbookForm(forms.Form):
|
||||
name = forms.CharField(label="Name")
|
||||
website = forms.CharField(label="Website", required=False)
|
||||
email = forms.EmailField(label="Email", required=False)
|
||||
comment = forms.CharField(label="Comment", required=False)
|
||||
capcha = forms.CharField(label="Tell me that you love 'me'")
|
||||
|
||||
def validate_capcha(self):
|
||||
if self.cleaned_data["capcha"].lower() != "i love me":
|
||||
raise ValidationError(txt('Invalid captcha'))
|
||||
return self
|
|
@ -1,6 +1,4 @@
|
|||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django.contrib import admin
|
||||
# Create your models here.
|
||||
|
||||
class Booksigner(models.Model):
|
||||
|
|
|
@ -41,28 +41,11 @@
|
|||
<p>
|
||||
<p>
|
||||
<form method="post" action="">
|
||||
<div>
|
||||
Name:
|
||||
<input type="text" name="name" />
|
||||
<span class="error">*</span>
|
||||
</div>
|
||||
<div>
|
||||
Website:
|
||||
<input type="text" name="website" />
|
||||
</div>
|
||||
<div>
|
||||
E-mail:
|
||||
<input type="text" name="email" />
|
||||
</div>
|
||||
<div>
|
||||
Comment:
|
||||
<textarea name="comment" rows="1"></textarea>
|
||||
</div>
|
||||
<p>
|
||||
Capcha(Say that you love "me"):
|
||||
<input type="text" name="capcha" />
|
||||
<span class="error">*</span>
|
||||
</p>
|
||||
{% csrf_token %}
|
||||
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
|
||||
<table>
|
||||
{{ form.as_table }}
|
||||
</table>
|
||||
<input type="submit" />
|
||||
</form>
|
||||
</p>
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from django.http import HttpResponseNotFound
|
||||
from django.http import HttpResponseNotFound, HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
import os, random
|
||||
|
||||
from .models import Booksigner
|
||||
from .forms import SignbookForm
|
||||
|
||||
def image_dict_from_name_list(name_list: list[str]):
|
||||
dict_list = []
|
||||
|
@ -45,9 +46,52 @@ def about(request):
|
|||
|
||||
def signbook(request):
|
||||
signer_list = Booksigner.objects.all()
|
||||
|
||||
context = {
|
||||
if request.method == 'POST':
|
||||
form = SignbookForm(request.POST)
|
||||
if form.is_valid():
|
||||
newsigner = {}
|
||||
newsigner["name"] = form.cleaned_data['name']
|
||||
newsigner["website"] = form.cleaned_data['website']
|
||||
newsigner["email"] = form.cleaned_data['email']
|
||||
newsigner["comment"] = form.cleaned_data['comment']
|
||||
host = request.META["SERVER_NAME"]
|
||||
print(host)
|
||||
match host:
|
||||
case "localhost":
|
||||
newsigner["hosting"] = "Local"
|
||||
case r".*\.fai.st":
|
||||
newsigner["hosting"] = "fai"
|
||||
case _:
|
||||
newsigner["hosting"] = "???"
|
||||
|
||||
signer_list = Booksigner.objects.all()
|
||||
if newsigner:
|
||||
Booksigner.objects.create(
|
||||
name=newsigner["name"],
|
||||
email=newsigner["email"],
|
||||
website=newsigner["website"],
|
||||
comment=newsigner["comment"],
|
||||
hosting=newsigner["hosting"],
|
||||
)
|
||||
context = {
|
||||
"title": "THE GUESTBOOK",
|
||||
"signer_list": signer_list
|
||||
}
|
||||
return render(request, "signbook.html", context)
|
||||
context = {
|
||||
"title": "THE GUESTBOOK",
|
||||
"signer_list": signer_list
|
||||
"signer_list": signer_list,
|
||||
"error_message": "You didn't input a required input.",
|
||||
}
|
||||
return render(request, "signbook.html", context)
|
||||
return render(
|
||||
request,
|
||||
"signbook.html",
|
||||
context
|
||||
)
|
||||
else:
|
||||
context = {
|
||||
"title": "THE GUESTBOOK",
|
||||
"signer_list": signer_list,
|
||||
"form": SignbookForm()
|
||||
}
|
||||
return render(request, "signbook.html", context)
|
||||
|
|
Loading…
Reference in New Issue