0

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

2
  • Is there any __init__.py file under dinners/views/? Commented May 7, 2012 at 23:02
  • Remove the * from the URL pattern (it has nothing to do with your error, but it's a good practice). Just leave r'^' if you want to match any url. Commented May 7, 2012 at 23:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I have those file (sorry for not including).. will update post with correct file structure
uhm, sorry for that brother, I thought you missed it and that might be making conflicts.
no worries, that was a good catch. I'm getting import errors in the python shell as well not a great start
Do you have a views.py file too? I mean, a views package and a views.py file inside "dinners" package?
Yeah. I have a views.py inside 'dinners' package, and a views package (which is located inside dinners package)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.