depot/web/quotes/quotedb/views.py

41 lines
1,010 B
Python

from django.shortcuts import get_object_or_404, render, redirect
from django.forms import ModelForm
from . import models
def home(request):
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
fields = ['quote']
def add_quote(request):
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})