0% found this document useful (0 votes)
45 views3 pages

Django Generic Views and Templates Guide

This document provides information on using generic class-based views in Django to build templates for listing, detailing, creating, and updating data from a model. It includes code snippets for setting up the views and templates, designing forms, and linking between pages. Key aspects covered are: 1. Defining generic views like ListView and DetailView and linking them to templates and URL paths. 2. Building templates to display lists and details of data using template tags and linking between records. 3. Creating a ModelForm class to generate forms from a model and customizing fields. 4. Configuring CreateView and UpdateView to work with the ModelForm and templates for creating and editing data.

Uploaded by

Marckhycs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

Django Generic Views and Templates Guide

This document provides information on using generic class-based views in Django to build templates for listing, detailing, creating, and updating data from a model. It includes code snippets for setting up the views and templates, designing forms, and linking between pages. Key aspects covered are: 1. Defining generic views like ListView and DetailView and linking them to templates and URL paths. 2. Building templates to display lists and details of data using template tags and linking between records. 3. Creating a ModelForm class to generate forms from a model and customizing fields. 4. Configuring CreateView and UpdateView to work with the ModelForm and templates for creating and editing data.

Uploaded by

Marckhycs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

HTML TEMPLATE

URL PATH {% for var in object_list %} BOOTSTRAP TO DESIGN YOUR PAGE


GENERIC VIEWS path(‘’, class Name.as_view(), name = “name”), #use this to get list of data
{{ [Link] in model}}
{% endfor %} BASE HTML TEMPLATE
from . models import model’s class name

from [Link] import ListView CSS to exdend to the other templates


URL PATH {{ block name }}
from [Link] import DetailView
path(‘name/<int:pk>’, class Name.as_view(), name = “name”),
{{ endblock }}

HTML TEMPLATE JS to extend to the other templates


class Name (ListView): DETAILED HTML TEMPLATE
{% for var in object_list %}
model = imported model <h1>{{ [Link] in model}}</h1> #create a link to navigate to detailed
#you can now reference to the template
template_name = “[Link]” detailed information of the data. <a href = “{% url ‘link name’ [Link] %}”>
{{ [Link] in model}} </a>
{% extends [Link] % }
{% endfor %}
class Name (DetailView):

model = imported model


PUTTING ALL THE FIELDS URL PATH TITLE LOGIC
template_name = “[Link]”
fields = ‘__all__’ path(‘name/’, class Name.as_view(), name = “name”),

Is html template {%
title %} is set?
GENERIC VIEWS PUTTING ALL THE FIELDS
HTML TEMPLATE
fields = (‘var name of field from the model
from . models import model’s class name <form method=”POST”>
you want to put and they are all separated <div class=form-group> Use the {% title %}
by commas’) Use the {% title %} in
from [Link] import CreateView {% csrf _token%} provided by the base
the html template
#actual form html template
{ {form.as_p}}
CREATE [Link] <button>BUTTON</button>
class Name (CreateView): </div>
model = imported model from django import forms </form> ADD POST URL TO [Link]
from . model import model class name from [Link] import reverse
template_name = “[Link]”
TWEAK [Link] def get_absolute_url(self):
class Name([Link]):
#designate what fields from model we want to indicate
class Meta: # for specific route with id
from . forms import class name Detail view link
model = model class name
fields = (‘var name of the field’) return reverse(link name, kwargs={“pk”:[Link]}
#remove from the class, the fields
widgets = { # for unspecific route without id
section and change it with
‘var name of field’ :
DESIGNING YOUR FORM [Link](attrs={‘class’: return reverse(link name)
form_class = import class name forms
WITH BOOTSTRAP ‘form-control’}),
GENERIC VIEWS
HTML TEMPLATE
PUTTING ALL THE FIELDS
from . models import model’s class name {% for var in object_list %}
URL PATH
fields = [‘var name of field from the model #use this to get list of data
from [Link] import UpdateView you want to put and they are all separated path(‘name/edit/<int:pk>’, class Name.as_view(), name = “name”), {{ [Link] in model}}
by commas’] <a href=”{% url ‘name’ [Link]%}”>Edit</a>
{% endfor %}
class Name (UpdateView):
HTML TEMPLATE HTML TEMPLATE
model = imported model URL PATH <form method=”POST”> <form method=”POST”>
template_name = “[Link]” path(‘name /<int:pk>/ delete’, class Name.as_view(), name = “name”), <div class=form-group> <div class=form-group>
{% csrf _token%}
#designate what fields from model we want to indicate {% csrf _token%} #actual form
{ {form.as_p}}
<button>BUTTON</button> <button>BUTTON</button>
</div> </div>
</form> </form>

GENERIC VIEWS

from . models import model’s class name

from [Link] import reverse_lazy

from [Link] import DeleteView

class Name (DeleteView):

model = imported model

template_name = “[Link]”

success_url = reverse_lazy(‘link name’)


App Name DetailView
[Link]

[Link] HTML templates CreateView


Project Name
ListView UpdateView
[Link]
[Link]

[Link]
[Link]

[Link]
CREATING THE PROJECT

PUSHING MODELS TO THE DB


CREATING THE PROJECT FOLDER MIGRATE COMMAND
~python [Link] makemigrations HTML TEMPLATES
Create virtual environment ~python [Link] migrate

~ python -m venv (name of venv) CREATING YOUR MODEL


Activating the venv ~from [Link] import User
RUNNING A SERVER
~(name of venv)\Scripts\activate ~python [Link] runserver class (name)([Link]):
Installing Django #input your models here
SETTING THE URL FOR
~pip install django def __str__(self): [Link]
Creating the actual Django Project CREATING SUPERUSER/ADMIN
return [Link] ~from . models import class
~python [Link] createsuperuser
~[Link] startproject (name) from django..[Link] (GEN)
REGISTER YOUR MODEL TO [Link]
class name (GEN):
BUILDING YOUR APP ~from .models import (model class name)
model = imported model
~python [Link] startapp (name) [Link](model class name)
template_name = “[Link]”
CONNECTING APP URL TO PROJECT URL

~from [Link] import path, include SETTING THE URL FOR [Link]
INSTALL YOUR APP IN [Link] ~from. import views
~urlpatterns = [
~just add the name of your app to the path(‘ ‘, class.as_view(), name = “name”)
[Link] >>> INSTALLED APPS [ ]; path(‘ ’, include(‘[Link]’),
hh
]

You might also like