我已经阅读了很多这样的问题,但我无法弄清楚我的No module named survatapp.models
:(
文件树如:
/survat #theproject
/survatapp #theapp
__init__.py
views.py
forms.py
models.py
这是追溯:
Environment:
Request Method: GET
Request URL: http://localhost:8000/list/
Django Version: 1.4.1
Python Version: 2.7.9
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'survat.survatapp')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
101. request.path_info)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
298. for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
328. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
323. self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
35. __import__(name)
File "C:\Users\Milexjaro\Dropbox\Thesis\survat\urls.py" in <module>
4. import survat.survatapp.views
File "C:\Users\Milexjaro\Dropbox\Thesis\survat\survatapp\views.py" in <module>
7. from survat.survatapp.models import Document
Exception Type: ImportError at /list/
Exception Value: No module named survatapp.models
这是views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from survat.survatapp.models import Document
from survat.survatapp.forms import DocumentForm
def list(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('survat.survatapp.views.list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'survatapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
这是我的models.py:
from django.forms.models import modelformset_factory
from django.db import models
from django.core.exceptions import ValidationError
def validate_file_extension(value):
if not value.name.endswith('.csv'):
raise ValidationError(u'Error message')
class Document(models.Model):
docfile = models.FileField(upload_to='documents', validators=[validate_file_extension])
更新:__init__.py
survat
我django-admin.py startproject addressbook .
运行/survat/survatapp
来构建项目。这就是为什么我认为/survat/survat/survatapp
代替survat.survatapp
。当我将INSTALLED_APPS中的survatapp
更改为survat.survatapp
并将views.py等中的所有survatapp
更改为No module named survatapp
时,我无法运行服务器,错误:{ {1}},我认为我的问题与this类似,在我的survatapp/urls.py
中就像:
from django.conf.urls import patterns, url
urlpatterns = patterns('survat.survatapp.views',
url(r'^list/$', 'list', name='list'),
)
我认为这很好,因为我使用了'list'
代替'survat.survatapp.views.list'
previous solution
注意:当时一切正常,它可以顺利运行,但是当我尝试在test.py
中导入views.py
时,它引发了错误,然后我删除了test.py
和导入代码在views.py
,我就像这样坚持了
2015年6月3日更新:添加了models.py