Msencenb
Msencenb

Reputation: 5104

Could not import app.models in view file

First django app and am having a bit of trouble my project is laid out like this

MyProject
  -dinners (python package, my app)
    -views (python package)
      __init__.py
      dinners.py(conflict was here... why didn't I add this to the q.. sigh)
      general.py
    __init__.py
    admin.py
    models.py
    tests.py
    views.py
  -Standard django project boilerplate

In my /views/general.py file it looks like this:

import os
import re
from django.http import HttpResponse
from django.template import Context,loader
from dinners.models import Dinner

def home(request):
    first_page_dinners = Dinner.objects.all().order_by('-created_at')[:5]
    t = loader.get_template('general/home.html')
    c = Context({
        'dinners':first_page_dinners,
    })
    return HttpResponse(t.render(Context()))

And in my urls.py file I have this regular expression to map to this view

url(r'^/*$','dinners.views.general.home', name="home")

However when I try to hit the home page I get this error: Could not import dinners.views.general. Error was: No module named models

Removing the dinners.models import at the top of general.py (plus all of the model specific code) removes the error. Am I somehow importing incorrectly into the view file? Naturally I need to be able to access my models from within the view...

Thanks

UPDATE: answer I had a file dinners.py within the -views package that was conflicting

Upvotes: 0

Views: 565

Answers (1)

santiagobasulto
santiagobasulto

Reputation: 11726

You need to put a __init__.py file in "dinners" and "views" folder to make those valid packages.

EDIT:

Also, remove that views.py file, that will create conflict with the package.

Upvotes: 1

Related Questions