0% found this document useful (0 votes)
86 views

Generic Views: Base HTML Template

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Generic Views: Base HTML Template

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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
{{ var.variable in model}}
{% endfor %} BASE HTML TEMPLATE
from . models import model’s class name

from django.views.generic import ListView CSS to exdend to the other templates


URL PATH {{ block name }}
from django.views.generic 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>{{ object.variable in model}}</h1> #create a link to navigate to detailed
#you can now reference to the template
template_name = “html.file” detailed information of the data. <a href = “{% url ‘link name’ post.pk %}”>
{{ var.variable in model}} </a>
{% extends base.html % }
{% endfor %}
class Name (DetailView):

model = imported model


PUTTING ALL THE FIELDS URL PATH TITLE LOGIC
template_name = “html.file”
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 django.views.generic import CreateView {% csrf _token%} provided by the base
the html template
#actual form html template
{ {form.as_p}}
CREATE FORMS.PY <button>BUTTON</button>
class Name (CreateView): </div>
model = imported model from django import forms </form> ADD POST URL TO MODELS.PY
from . model import model class name from django.urls import reverse
template_name = “html.file”
TWEAK VIEWS.PY def get_absolute_url(self):
class Name(forms.ModelForm):
#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”:self.pk}
#remove from the class, the fields
widgets = { # for unspecific route without id
section and change it with
‘var name of field’ :
DESIGNING YOUR FORM forms.fieldtype(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 django.views.generic import UpdateView you want to put and they are all separated path(‘name/edit/<int:pk>’, class Name.as_view(), name = “name”), {{ var.variable in model}}
by commas’] <a href=”{% url ‘name’ post.pk%}”>Edit</a>
{% endfor %}
class Name (UpdateView):
HTML TEMPLATE HTML TEMPLATE
model = imported model URL PATH <form method=”POST”> <form method=”POST”>
template_name = “html.file” 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 django.urls import reverse_lazy

from django.views.generic import DeleteView

class Name (DeleteView):

model = imported model

template_name = “html.file”

success_url = reverse_lazy(‘link name’)


App Name DetailView
django.views.generic

views.py HTML templates CreateView


Project Name
ListView UpdateView
urls.py
settings.py

urls.py
models.py

admin.py
CREATING THE PROJECT

PUSHING MODELS TO THE DB


CREATING THE PROJECT FOLDER MIGRATE COMMAND
~python manage.py makemigrations HTML TEMPLATES
Create virtual environment ~python manage.py migrate

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


Activating the venv ~from django.contrib.auth.models import User
RUNNING A SERVER
~(name of venv)\Scripts\activate ~python manage.py runserver class (name)(models.Model):
Installing Django #input your models here
SETTING THE URL FOR
~pip install django def __str__(self): VIEWS.PY
Creating the actual Django Project CREATING SUPERUSER/ADMIN
return self.attr ~from . models import class
~python manage.py createsuperuser
~django-admin.py startproject (name) from django..views.generic (GEN)
REGISTER YOUR MODEL TO ADMIN.PY
class name (GEN):
BUILDING YOUR APP ~from .models import (model class name)
model = imported model
~python manage.py startapp (name) admin.site.register(model class name)
template_name = “html.file”
CONNECTING APP URL TO PROJECT URL

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

You might also like