Inicio Information Technology Dynamic internet apps with HTMX, Python, and Django

Dynamic internet apps with HTMX, Python, and Django

0
Dynamic internet apps with HTMX, Python, and Django




$ 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 must work together with them immediately.) The migrate command applies the modifications.

Setting up the view

Subsequent up, let’s think about the view, which accepts a request and prepares the mannequin (if vital), 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.technique == '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 knowledge 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 checklist on the entrance finish.

DEJA UNA RESPUESTA

Por favor ingrese su comentario!
Por favor ingrese su nombre aquí