Generic Views: Base HTML Template
Generic Views: Base HTML Template
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
template_name = “html.file”
urls.py
models.py
admin.py
CREATING THE PROJECT
~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
]