$ python handle.py makemigrations $ python handle.py migrate
The makemigrations
command creates a brand new migration file if any schema modifications are detected. (These are present in quoteapp/migrations
, however you received’t sometimes have to work together with them immediately.) The migrate
command applies the modifications.
Developing the view
Subsequent up, let’s take into account the view, which accepts a request and prepares the mannequin (if mandatory), and arms 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 want coming from the shopper. 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 checklist on the entrance finish.