diff --git a/contacts/forms.py b/contacts/forms.py
index 78d33eb..bad8c1b 100644
--- a/contacts/forms.py
+++ b/contacts/forms.py
@@ -1,7 +1,7 @@
from django import forms
from .models import Contact
-class ContactForm(forms.modelForm):
+class ContactForm(forms.ModelForm):
class Meta:
model = Contact
fields = ('name', 'age',)
\ No newline at end of file
diff --git a/contacts/templates/contacts/contact_detail.html b/contacts/templates/contacts/contact_detail.html
index 535d108..68a1ea3 100644
--- a/contacts/templates/contacts/contact_detail.html
+++ b/contacts/templates/contacts/contact_detail.html
@@ -1,6 +1,7 @@
{% extends 'contacts/base.html' %} {% block content %}
-
{{ contact.name }} (edit)
+{{ contact.name }} (edit)
Age: {{ contact.age }}
+Delete contact
{% endblock %}
\ No newline at end of file
diff --git a/contacts/templates/contacts/contact_form.html b/contacts/templates/contacts/contact_form.html
new file mode 100644
index 0000000..01c37c6
--- /dev/null
+++ b/contacts/templates/contacts/contact_form.html
@@ -0,0 +1,8 @@
+
+{% extends 'contacts/base.html' %} {% block content %}
+New Contact
+
+{% endblock %}
\ No newline at end of file
diff --git a/contacts/templates/contacts/contact_list.html b/contacts/templates/contacts/contact_list.html
index dec0996..8e14958 100644
--- a/contacts/templates/contacts/contact_list.html
+++ b/contacts/templates/contacts/contact_list.html
@@ -1,7 +1,7 @@
{% extends 'contacts/base.html' %} {% block content %}
-Contacts (+)
+Contacts (+)
{% for contact in contacts %}
-
diff --git a/contacts/urls.py b/contacts/urls.py
index a381040..5406ca8 100644
--- a/contacts/urls.py
+++ b/contacts/urls.py
@@ -4,4 +4,7 @@ from . import views
urlpatterns = [
path('', views.contact_list, name='contact_list'),
path('contacts/', views.contact_detail, name='contact_detail'),
+ path('contacts/new', views.contact_create, name='contact_create'),
+ path('contacts//edit', views.contact_edit, name='contact_edit'),
+ path('contacts//delete', views.contact_delete, name='contact_delete')
]
\ No newline at end of file
diff --git a/contacts/views.py b/contacts/views.py
index a37f7e4..cee5c33 100644
--- a/contacts/views.py
+++ b/contacts/views.py
@@ -1,6 +1,7 @@
-from django.shortcuts import render
+from django.shortcuts import render, redirect
from .models import Contact
+from .forms import ContactForm
# Create your views here.
@@ -12,4 +13,32 @@ def contact_list(request):
## show
def contact_detail(request, pk):
contact = Contact.objects.get(id=pk)
- return render(request, 'contacts/contact_detail.html', {'contact': contact})
\ No newline at end of file
+ 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')