$ python handle.py makemigrations
$ python handle.py migrate
The makemigrations
command creates a brand new migration file if any schema adjustments are detected. (These are present in quoteapp/migrations
, however you gained’t usually must work together with them immediately.) The migrate
command applies the adjustments.
Developing the view
Subsequent up, let’s contemplate the view, which accepts a request and prepares the mannequin (if mandatory), and fingers it off to be rendered as a response. We’ll solely want one view, discovered at quoteapp/views.py
:
// cat quoteapp/views.py
from django.shortcuts import render
from django.template.loader import render_to_string
from django.http import HttpResponse
from .fashions import Quote
def index(request):
if request.methodology == 'POST':
textual content = request.POST.get('textual content')
creator = request.POST.get('creator')
if textual content and creator:
new_quote = Quote.objects.create(textual content=textual content, creator=creator)
# Render the brand new quote HTML
html = render_to_string('quoteapp/quote_item.html', {'quote': new_quote})
return HttpResponse(html)
quotes = Quote.objects.all()
return render(request, 'quoteapp/index.html', {'quotes': quotes})
On this view, we import the Quote mannequin and use it to craft a response within the index
operate. The request
argument offers us entry to all the data we’d like coming from the consumer. If the strategy is POST
, we assemble a brand new Quote
object and use Quote.objects.create()
to insert it into the database. As a response, we ship again simply the markup for the brand new quote, as a result of HTMX will insert it into the listing on the entrance finish.