What are Django Views?

Search for a command to run...

No comments yet. Be the first to comment.
This is a basic Django tutorial in which we will create an Authentication App, from scratch in Django 2.0 and Learn the Fundamental concepts of Django Framework.
In this section, we will learn what are Templates in Django and where to store templates or HTML pages in Django. Along with Templates, we will also see how to handle and store Static files in Django. Static files include Javascript, CSS, Images. So ...
You've done it. I've done it. We've all done it.

Your AI Agent Has Amnesia. ByteRover Fixes It — Without Vectors Let's see how to built a customer support agent that remembers everything — using plain markdown files, BM25 search, zero embeddings, a

My GenAI cohort kicked off today, and what a first day! Right away we were reminded that AI isn’t magic — at its core, models like Generative Pretrained Transformers (GPT) are doing one simple thing:

Hello Folks👋, I Recently got Graduated🎓 in 2020, yep in the Pandemic. We all hit very hard by this Corona Pandemic and we all got stuck at home for very long. We all realize the Importance of Socializing, Fitness (Physically as well Mentally), Diet...

So after learning the basic commands of git in our previous section, let's see Git in action. If you already have a project to track your files then you can skip this section and if not then continue. Initializing a Repository from Non-existing Dire...

In this section, we will learn what are Views in Django and how to use views.py to display webpage onto the browser on a web request.
So in our previous section, we have successfully created our UserCreateForm form. Let's see what are Views in Django.
A View function, or "view" for short, is simply a Python function that is written in views.py file, that takes a web request and returns a web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, etc. So to display a webpage we require two things:
A URL associated with the view function
A Template (HTML page) which is to be displayed
Let's write the first view. Open the file account/views.py and put the following Python code in it:
from django.shortcuts import render, redirect
from django.views.generic.base import View
from django.contrib.auth import login, authenticate
from . import forms
# Create your views here.
#this is a class based view
class IndexView(View):
def get(self, request, *args, **kwargs):
return render(request, "registration/index.html")
#this is a function based view
def signup(request):
if request.method == 'POST':
form = forms.UserCreateForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=raw_password)
login(request, user)
return redirect('index')
else:
form = forms.UserCreateForm()
return render(request, 'registration/signup.html', {'form': form})
That's it! we have written our first view. Next, we need to create a URL mapping for these functions and templates for each view function, so will see them in detail in our next tutorial.
In this section, we have successfully writtenView in Django. So in the next section, we will see what are **Templates** in Django.