Last active
July 16, 2024 23:04
-
-
Save roymanigley/207fda9ff52fbfce688d20cbdd7ebb23 to your computer and use it in GitHub Desktop.
Django ModelForm Template Views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from core.models import Person, Address | |
from django.forms import ModelForm, widgets | |
from django.views.generic.edit import UpdateView, CreateView, DeleteView | |
from django.views.generic.list import ListView | |
class PersonForm(ModelForm): | |
class Meta: | |
model = Person | |
fields = '__all__' | |
widgets = { | |
'birthday': widgets.DateInput(attrs={'type': 'date'}) | |
} | |
class PersonCreateView(CreateView): | |
form_class = PersonForm | |
template_name = 'create_update.html' | |
success_url = '/person' | |
class PersonUpdateView(UpdateView): | |
form_class = PersonForm | |
model = Person | |
template_name = 'create_update.html' | |
success_url = '/person' | |
class PersonDeleteView(DeleteView): | |
model = Person | |
template_name = 'delete.html' | |
success_url = '/person' | |
class PersonListView(ListView): | |
template_name = 'list.html' | |
model = Person | |
context_object_name = 'records' | |
''' | |
# TEMPLATES | |
## create_update.html | |
{% extends 'base.html'%} | |
{% block content %} | |
<form method='POST'> | |
{% csrf_token %} | |
{{ form.as_p }} | |
<button type="submit">save</button> | |
</form> | |
{% endblock content %} | |
## delete.html | |
{% extends 'base.html' %} | |
{% block content %} | |
<center> | |
<p>confirm delete</p> | |
<form method='POST'> | |
{% csrf_token %} | |
<button class="btn danger" type="submit">save</button> | |
</form> | |
</center> | |
{% endblock content %} | |
## list.html | |
{% extends 'base.html' %} | |
{% block content %} | |
<table> | |
<thead> | |
<th> | |
<a class="btn primary" href="new/">add</a> | |
</th> | |
<th></th> | |
<th></th> | |
</thead> | |
<tbody> | |
{% for r in records %} | |
<tr> | |
<td><a href="{{ r.id }}/">{{ r.id }}</a></td> | |
<td>{{ r }}</td> | |
<td><a class="btn danger" href="{{ r.id }}/delete">delete</a></td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
{% endblock content %} | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment