depot/web/quotes/quotedb/views.py

41 lines
1,009 B
Python
Raw Normal View History

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,
},
)
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"]
def add_quote(request):
2021-01-20 22:48:41 +00:00
if request.method == "POST":
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})