The solution to this particular error has been evading me for about three days now. I have been searching on google and stackoverflow etc but have not found anything that answers my problem.
def contact_detail(request, slug):
# grab the object...
contact = Contact.objects.get(slug=slug)
# and pass to the template
return render(request, 'contact_detail', {'contact': contact, })
The error seems to point to line above with the contact variable. The error occurs as soon as I click âcreate contactâ in index.html. Iâd really appreciate some assistance. I am thinking it is a stupid simple mistake I have made. Iâm not even sure how to do a slug with this or even whether I need one.
The model this relates to is as follows:-
class Contact(models.Model):
firstname = models.CharField(max_length = 50, blank = False, null = False)
surname = models.CharField(max_length = 50, blank = False, null = False)
address1 = models.CharField(max_length = 50, blank = False, null = False)
address2 = models.CharField(max_length = 50, blank = True, null = True)
address3 = models.CharField(max_length = 50, blank = True, null = True)
town = models.CharField(max_length = 50, blank = False, null = False)
county = models.CharField(max_length = 50, blank = False, null = False)
country = models.CharField(max_length = 50, blank = True, null = True)
postcode = models.CharField(max_length = 10, blank = False, null = False)
telephone = models.CharField(max_length = 30, blank = True, null = True)
mobile = models.CharField(max_length = 30, blank = False, null = False)
email = models.EmailField(null = True, blank = True)
birthday = models.DateField(blank = True, null = True)
birthdaycardsent = models.BooleanField(default = False)
xmascardreceived = models.BooleanField(default = False)
xmascardsent = models.BooleanField(default = False)
notes = models.TextField(null = True, blank = True)
slug = models.SlugField(unique = True)
def __str__ (self):
return self.firstname + ' ' + self.surname
def get_absolute_url(self):
return "/contacts/%s/" % self.slug
urls:-
from django.conf import settings
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from contacts import views
urlpatterns = [
url(r'^$', views.index, name = 'home'),
url(r'^contacts/$', views.contacts, name = 'contacts'),
url(r'^about/$', TemplateView.as_view(template_name='about.html'), name='about'),
url(r'^contact/$', TemplateView.as_view(template_name='contact.html'), name='contact'),
url(r'^contacts/(?P<slug>[-\w]+)/$', views.contact_detail, name='contact_detail'),
url(r'^contacts/(?P<slug>[-\w]+)/edit/$', views.edit_contact, name='edit_contact'),
url(r'^contacts/create_contact/$', views.create_contact, name='create_contact'),
url(r'^admin/', admin.site.urls),
]
index.html:-
{% extends 'base.html' %}
{% block title %}
Homepage - {{ block.super }}
{% endblock title %}
{% block content %}
{% for contact in contacts %}
<h2><a href="{% url 'contact_detail' slug=contact.slug %}">
{{ contact.firstname }} {{ contact.surname }}
</a></h2>
<p>{{ contact.birthday }}</p>
{% endfor %}
<a href="{% url 'create_contact' %}">Create new contact</a>
<a href="{% url 'home' %}">Home</a>
{% endblock content %}