0Pricing
Django Academy · Lezione

Scrivere una view basata su funzione

Crei una view che restituisce una risposta

Scrivere una view basata su funzione è una lezione Django Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Django Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Django Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

What a View Really Is

A Django view is just a Python function that takes a request and returns a response. That is the whole contract. 🙂

The First Argument: request

Every view's first parameter is the request object. Django hands it to you automatically when a URL matches your view.

Importing HttpResponse

To send something back, you return an HttpResponse. Import it from django.http before you use it.

from django.http import HttpResponse

Your First View

Here is a complete, working view. It takes request and returns plain text in a response.

from django.http import HttpResponse

def home(request):
    return HttpResponse('Hello, Django!')

Where Views Live

By convention, views go in your app's views.py file. Keeping them there makes them easy to find and wire up later.

You Must Return Something

A view that returns None raises an error. Django expects an HttpResponse every single time, with no exceptions.

Returning HTML

The response body is just a string, so you can put HTML inside it. The browser renders it like any page.

def home(request):
    return HttpResponse('<h1>Welcome</h1>')

Views Are Plain Functions

A view is normal Python, so you can run logic inside it: build a message, check a condition, then return your response.

def greet(request):
    name = 'friend'
    return HttpResponse('Hi ' + name)

Naming Your View

Name views after what they do, like home or about. Clear names make your URL file readable when you connect them.

Function-Based vs Class-Based

This style is a function-based view, the simplest kind. Class-based views come later; functions are perfect to start with.

Nothing Runs Until It Is Mapped

A view does nothing on its own. It only runs when a URL points to it, which is the very next step you will learn.

Quick Check

Let's confirm the core contract of a Django view.

Recap: You Wrote a View

A view is a function that takes request and returns an HttpResponse. You import it, write your logic, and live in views.py. 🎉

Domande Frequenti

La lezione «Scrivere una view basata su funzione» è gratuita?

Sì — il testo completo di «Scrivere una view basata su funzione» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Django Academy, passa a CoddyKit PRO. Il corso Django Academy include 4 lezioni in totale.

Cosa imparerò in «Scrivere una view basata su funzione»?

Crei una view che restituisce una risposta Eserciti Django Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Django Academy?

Non è richiesta alcuna esperienza precedente. Django Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.

Quanto tempo richiede la lezione «Scrivere una view basata su funzione»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Django Academy?

Sì. Ogni lezione Django Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Scrivere una view basata su funzione
  2. Pattern URL con path()
  3. Catturare i parametri degli URL
  4. Includere gli URL delle app e assegnare nomi alle route
← Torna a Django Academy