2021-01-20 22:48:15 +00:00
|
|
|
from django.shortcuts import get_object_or_404, render, redirect
|
|
|
|
from django.forms import ModelForm
|
2021-01-19 03:51:22 +00:00
|
|
|
|
2021-01-20 22:20:07 +00:00
|
|
|
from . import models
|
|
|
|
|
2021-01-19 03:51:22 +00:00
|
|
|
|
|
|
|
def home(request):
|
2021-01-20 22:20:07 +00:00
|
|
|
random_quote = models.Quote.objects.all().order_by("?").first()
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"quotedb/home.html",
|
|
|
|
{
|
|
|
|
"quote": random_quote,
|
|
|
|
},
|
|
|
|
)
|
2021-01-20 22:48:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def show_quote(request, quote_id):
|
|
|
|
quote = get_object_or_404(models.Quote, id=int(quote_id))
|
|
|
|
return render(request, "quotedb/show_quote.html", {"quote": quote})
|
|
|
|
|
|
|
|
|
|
|
|
class AddQuoteForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = models.Quote
|
2021-01-20 22:48:41 +00:00
|
|
|
fields = ["quote"]
|
2021-01-20 22:48:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_quote(request):
|
2021-01-20 22:48:41 +00:00
|
|
|
if request.method == "POST":
|
2021-01-20 22:48:15 +00:00
|
|
|
form = AddQuoteForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
new_quote = form.save(commit=False)
|
|
|
|
new_quote.added_by = request.user
|
|
|
|
new_quote.save()
|
|
|
|
return redirect(new_quote)
|
|
|
|
else:
|
|
|
|
form = AddQuoteForm()
|
|
|
|
|
|
|
|
return render(request, "quotedb/new.html", {"form": form})
|