create, edit, delete, forms

master
Jerrica Bobadilla 5 years ago
parent b8c6793d19
commit be05b7fb81

@ -1,7 +1,7 @@
from django import forms from django import forms
from .models import Contact from .models import Contact
class ContactForm(forms.modelForm): class ContactForm(forms.ModelForm):
class Meta: class Meta:
model = Contact model = Contact
fields = ('name', 'age',) fields = ('name', 'age',)

@ -1,6 +1,7 @@
<!-- including the base.html as a boilerplate --> <!-- including the base.html as a boilerplate -->
{% extends 'contacts/base.html' %} {% block content %} {% extends 'contacts/base.html' %} {% block content %}
<!-- contact detail content --> <!-- contact detail content -->
<h2>{{ contact.name }} <a href="">(edit)</a></h2> <h2>{{ contact.name }} <a href="{% url 'contact_edit' pk=contact.pk %}"}>(edit)</a></h2>
<h4>Age: {{ contact.age }}</h4> <h4>Age: {{ contact.age }}</h4>
<a href="{% url 'contact_delete' pk=contact.pk %}">Delete contact</a>
{% endblock %} {% endblock %}

@ -0,0 +1,8 @@
<!-- tunr/templates/tunr/artist_form.html -->
{% extends 'contacts/base.html' %} {% block content %}
<h2>New Contact</h2>
<form method="POST">
{% csrf_token %} {{ form.as_p }}
<button type="submit">Save</button>
</form>
{% endblock %}

@ -1,7 +1,7 @@
<!-- including the base.html as a boilerplate --> <!-- including the base.html as a boilerplate -->
{% extends 'contacts/base.html' %} {% block content %} {% extends 'contacts/base.html' %} {% block content %}
<!-- contact list content --> <!-- contact list content -->
<h2>Contacts <a href="">(+)</a></h2> <h2>Contacts <a href="{% url 'contact_create' %}">(+)</a></h2>
<ul> <ul>
{% for contact in contacts %} {% for contact in contacts %}
<li> <li>

@ -4,4 +4,7 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.contact_list, name='contact_list'), path('', views.contact_list, name='contact_list'),
path('contacts/<int:pk>', views.contact_detail, name='contact_detail'), path('contacts/<int:pk>', views.contact_detail, name='contact_detail'),
path('contacts/new', views.contact_create, name='contact_create'),
path('contacts/<int:pk>/edit', views.contact_edit, name='contact_edit'),
path('contacts/<int:pk>/delete', views.contact_delete, name='contact_delete')
] ]

@ -1,6 +1,7 @@
from django.shortcuts import render from django.shortcuts import render, redirect
from .models import Contact from .models import Contact
from .forms import ContactForm
# Create your views here. # Create your views here.
@ -13,3 +14,31 @@ def contact_list(request):
def contact_detail(request, pk): def contact_detail(request, pk):
contact = Contact.objects.get(id=pk) contact = Contact.objects.get(id=pk)
return render(request, 'contacts/contact_detail.html', {'contact': contact}) return render(request, 'contacts/contact_detail.html', {'contact': contact})
## new
def contact_create(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
contact = form.save()
return redirect('contact_detail', pk=contact.pk)
else:
form = ContactForm()
return render(request, 'contacts/contact_form.html', {'form': form})
## edit
def contact_edit(request, pk):
contact = Contact.objects.get(id=pk)
if request.method == "POST":
form = ContactForm(request.POST, instance=contact)
if form.is_valid():
contact = form.save()
return redirect('contact_detail', pk=contact.pk)
else:
form = ContactForm(instance=contact)
return render(request, 'contacts/contact_form.html', {'form': form})
## delete
def contact_delete(request, pk):
Contact.objects.get(id=pk).delete()
return redirect('contact_list')

Loading…
Cancel
Save