Forums

ViewDoesNotExist at /newcontacts/

here is an issue I have been having. The model does not exist but it definitely does. I have check for typo's and I have concluded that there is an error either in my code or somewhere else.... because I definitely have a directory called 'newcontacts' in my mysite directory, as wellls as a views.py in the newcontacts directory.

I have no idea what is going wrong. Any help is appreciated.

I am following this tutorial to try and accept and save contact information from a form. "http://www.peachybits.com/2011/09/django-1-3-form-api-modelform-example/"

the urls.py

urlpatterns += patterns('mysite.newcontacts',
    url(r'^newcontacts/$', 'views.contacts'),
    url(r'^contact_add/$', 'views.contact_add'),
)

I'd guess that there's no function called contacts in your file mysite/newcontacts/views.py. If that's definitely there, is it possible that you've not reloaded your web app since adding it?

Thanks I will double check again. but for now the site is down again with a "something went wrong".

This is my view.py in mysite.newcontacts directory.

from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.template import RequestContext

from mysite.newcontacts.models import Contact, PhoneNo
from mysite.newcontacts.forms import ContactForm, PhoneNoForm

def contacts(request):
    latest_contact_list = Contact.objects.all().order_by('name')

    return render_to_response('mysite/newcontacts/templates/contacts.html',
    {'latest_contact_list': latest_contact_list,})

def contact_add(request):
    # sticks in a POST or renders empty form
    form = ContactForm(request.POST or None)
    if form.is_valid():
        cmodel = form.save()
        #This is where you might chooose to do stuff.
        #cmodel.name = 'test1'
        cmodel.save()
        return redirect(contacts)

    return render_to_response('newcontacts/templates/contact_add.html',
                              {'contact_form': form},
                              context_instance=RequestContext(request))

def contact_edit(request, contact_id):
    contact = get_object_or_404(Contact, pk=contact_id)
    form = ContactForm(request.POST or None, instance=contact)
    if form.is_valid():
        contact = form.save()
        #this is where you might choose to do stuff.
        #contact.name = 'test'
        contact.save()
        return redirect(contacts)

    return render_to_response('newcontacts/templates/contact_edit.html',
                              {'contact_form': form,
                               'contact_id': contact_id},
                              context_instance=RequestContext(request))

def contact_delete(request, contact_id):
    c = Contact.objects.get(pk=contact_id).delete()

    return redirect(contacts)

The site looks OK to me right now. And that views.py looks fine.

One wild possibility -- did you create the newcontacts directly (instead of using Django's startapp command) and forget to put an empty file called __init__.py in it?

That is very true. wow sry.

Aha! Glad we tracked it down eventually :-)