Forums

ViewDoesNotExist Exception

Hi So I followed the Django 1.7 tutorial and I'm getting a view does not exist exception.

Exception Value:    
Could not import registration.views.home. View does not exist in module registration.views.
Exception Location: /home/TannerBaldus/.virtualenvs/venv/lib/python2.7/site-packages/django/core/urlresolvers.py 
in   get_callable, line 118  
Python Executable:  /usr/local/bin/uwsgi

Importing registration.views.home using both python manage.py shell and python -i /var/www/tannerbaldus_pythonanywhere_com_wsgi.py work. In fact all my urls get the same error and all of my views import correctly in the shell. Here's my urls.py:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'registration.views.home'),
    url(r'^ibsb', 'registration.views.instructor_by_subject'),
    url(r'^ibsc', 'registration.views.instructor_by_score'),
    url(r'^ibn', 'registration.views.instructor_by_name'),

    url(r'^obsc', 'registration.views.offering_by_score'),
    url(r'^obsb', 'registration.views.offering_by_subject'),
    url(r'^obi', 'registration.views.offering_by_instructor'),

    url(r'^ebi', 'registration.views.eval_by_instructor'),
    url(r'^ebsc', 'registration.views.eval_by_score'),
)

views.py:

from django.shortcuts import render
import models as m


def home(request):
    return render(request, 'search.html')


def get_qury_dict(request):
    if request.method == 'GET':
        return request.GET
    elif request.method == 'POST':
        return request.POST


def serialize(obj):
    return obj.__unicode__()


def serailize_query(query):
    return map(serialize, query)


def short_render(request, query):
    """
    Since we're returning the same template this weiil cut down on retyping everything.
    :param query:
    :return:
    """
    return render(request, 'results.html', {'results': serailize_query(query)})


def instructor_by_subject(request):
    qd = get_qury_dict(request)
    subject_code = qd['sub_code'].upper()
    offerings = m.Offering.objects.filter(course__subject__code=subject_code)
    instructors = list(set(map(lambda o: "{} {}".format(o.instructor.fname, o.instructor.lname), offerings)))
    return render(request, 'results.html', {'results': instructors})


def instructor_by_score(request):
    qd = get_qury_dict(request)
    rating = float(qd['score'])
    eval_fn = m.Evaluation.objects.avg_ratings
    instructors = m.Instructor.objects.search_by_evaluation(rating, eval_fn)
    return short_render(request, instructors)


def instructor_by_name(request):
    qd = get_qury_dict(request)
    fname = qd['fname']
    lname = qd['lname']
    instructors = m.Instructor.objects.filter(fname=fname, lname=lname)
    return short_render(request, instructors)


def offering_by_score(request):
    qd = get_qury_dict(request)
    score = float(qd['score'])
    eval_fn = m.Evaluation.objects.avg_ratings
    offerings = m.Offering.objects.search_by_evaluation(score, eval_fn)
    return short_render(request, offerings)


def offering_by_subject(request):
    qd = get_qury_dict(request)
    subject_code = qd['sub_code']
    offerings = m.Offering.objects.filter(course__subject__code=subject_code)
    return short_render(request, offerings)


def offering_by_instructor(request):
    qd = get_qury_dict(request)
    fname = qd['fname']
    lname = qd['lname']
    offerings = m.Offering.objects.filter(instructor__fname=fname, instructor__lname=lname)
    return short_render(request, offerings)


def eval_by_score(request):
    qd = get_qury_dict(request)
    score = qd['score']
    evals = m.Evaluation.objects.by_score(score)
    return short_render(request, evals)

def evals_by_instructor(request):
    qd = get_qury_dict(request)
    fname = qd['fname']
    lname = qd['lname']
    offerings = m.Evaluation.objects.filter(instructor__fname=fname, instructor__lname=lname)
    return short_render(request, offerings)

wsgi.py

# This file contains the WSGI configuration required to serve up your
# web application at http://TannerBaldus.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# Below are templates for Django and Flask.  You should update the file
# appropriately for the web framework you're using, and then
# click the 'Reload /yourdomain.com/' button on the 'Web' tab to make your site
# live.

# +++++++++++ VIRTUALENV +++++++++++
# If you're using a virtualenv, you should activate it at the top of your
# wsgi file, using code like this:
#
#activate_this_path = '/home/TannerBaldus/path/to/my-virtualenv/bin/activate_this.py'
#with open(activate_this_path) as f:
#    code = compile(f.read(), activate_this_path, 'exec')
#exec(code, dict(__file__=activate_this_path))



# +++++++++++ CUSTOM WSGI +++++++++++
# If you have a WSGI file that you want to serve using PythonAnywhere, perhaps
# in your home directory under version control, then use something like this:
#
#import os
#import sys
#
#path = '/home/TannerBaldus/path/to/my/app
#if path not in sys.path:
#    sys.path.append(path)
#
#from my_wsgi_file import application


# +++++++++++ DJANGO +++++++++++
HOME = '/home/TannerBaldus'  # insert your own username here
activate_this = HOME + '/.virtualenvs/venv/bin/activate_this.py'
with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))

import os
import sys

project_path = HOME + '/better_register/better_register'
if project_path not in sys.path:
    sys.path.append(project_path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'better_register.settings'

import django
django.setup()

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


# +++++++++++ FLASK +++++++++++
# Here is a simple Flask app. It is currently serving the default welcome
# page.  You can adapt this if you want.
#
# If you're using a different web framework, you'll need to comment all of this
# out

# from flask import Flask

# application = Flask(__name__)

# @application.route('/')
# def hello_world():
#     return """
#             <html>
#             <head>
#                 <title>Python Anywhere hosted web application</title>
#             </head>
#             <body>
#             <h1>Hello, World!</h1>
#             <p>
#                 This is the default welcome page for a
#                 <a href="https://www.pythonanywhere.com/">PythonAnywhere</a>
#                 hosted web application.
#             </p>
#             <p>
#                 Find out more about how to configure your own web application
#                 by visiting the <a href="https://www.pythonanywhere.com/web_app_setup/">web app setup</a> page
#             </p>
#             </body>
#             </html>"""

Hi TannerBaldus,

Django already has a module called registration. your app name is clashing with it. You should just change it to some other name.

Thanks, Conrad I'll do that :). However I was ended up getting my project up on an Amazon microinstance. Out of curiosity, is there something in the PythonAnywhere implementation that catches that clash that the standard mod_wsgi/apache deployment doesn't?

It is most likely because on your amzn deployment, you imported the django registration module first, and then imported your app registration, so standard django registration got overwritten by your app registration.

This means that if you use django registration later, you will get an error then. vs. at PythonAnywhere setup, you imported app registration and then imported django registration, so you got an error right away when trying to access app registration stuff.

This is pretty arbitrary and not robust. I guess theoretically we do error catch/fail faster since we import project_home first...