2015-06-13 48 views
0

我的Django項目的文件結構導入錯誤在models.py形成

myProject 
     |--newsletter 
      |--__init__.py 
      |--admin.py 
      |--forms.py 
      |--models.py 
      |--tests.py 
      |--urls.py 
      |--views.py 
     |--myproject 
      |--settings.py 
      |--urls.py 
      |--wsgi.py 

     |--db.sqlite3 
     |--manage.py 

代碼

from django.db import models 
class SignUp(models.Model): 

    email = models.EmailField() 
    first_name = models.CharField(max_length=120) 
    last_name = models.CharField(max_length=120, blank=True) 
    subscribtion_type_choices = (
     ('W', 'Weekly'), 
     ('M', 'Monthly'), 
     ('Y', 'Yearly') 
    ) 
    subscribtion_type = models.CharField(max_length=1, choices=subscribtion_type_choices) 

    def __str__(self): 
     return self.first_name 

代碼admin.py

from django.contrib import admin 
from newsletter.forms import SignUpForm 
from newsletter.models import SignUp 

class SignUpAdmin(admin.ModelAdmin): 
    # list_display = ('first_name', 'last_name', 'email', 'subscribtion_type') 
    form_signup = SignUpForm 

admin.site.register(SignUp, SignUpAdmin) 

代碼forms.py

from django import forms 
from newsletter.models import SignUp 

class SignUpForm(forms.ModelForm): 

    class Meta: 
     model = SignUp 
     fields = ['first_name', 'last_name', 'email', 'subscribtion_type', ] 

當我打在崇高的文本Ctrl + B我得到一個導入錯誤說在form.py

回溯沒有模塊newsletter.models爲form.py

Traceback (most recent call last): 
    File "/home/darshan99/django101/trydjango18/newsletter/forms.py", line 2, in <module> 
    from newsletter.models import SignUp 
ImportError: No module named newsletter.models 
[Finished in 0.1s with exit code 1] 
[shell_cmd: python -u "/home/darshan99/django101/trydjango18/newsletter/forms.py"] 
[dir: /home/darshan99/django101/trydjango18/newsletter] 
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games] 

回溯在admin.py

Traceback (most recent call last): 
    File "/home/darshan99/django101/trydjango18/newsletter/admin.py", line 1, in <module> 
    from django.contrib import admin 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 4, in <module> 
    from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/helpers.py", line 8, in <module> 
    from django.contrib.admin.utils import (
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/utils.py", line 7, in <module> 
    from django.contrib.auth import get_permission_codename 
    File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py", line 7, in <module> 
    from django.middleware.csrf import rotate_token 
    File "/usr/local/lib/python2.7/dist-packages/django/middleware/csrf.py", line 14, in <module> 
    from django.utils.cache import patch_vary_headers 
    File "/usr/local/lib/python2.7/dist-packages/django/utils/cache.py", line 26, in <module> 
    from django.core.cache import caches 
    File "/usr/local/lib/python2.7/dist-packages/django/core/cache/__init__.py", line 34, in <module> 
    if DEFAULT_CACHE_ALIAS not in settings.CACHES: 
    File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__ 
    self._setup(name) 
    File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup 
    % (desc, ENVIRONMENT_VARIABLE)) 
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 
[Finished in 0.2s with exit code 1] 
[shell_cmd: python -u "/home/darshan99/django101/trydjango18/newsletter/admin.py"] 
[dir: /home/darshan99/django101/trydjango18/newsletter] 
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games] 
+1

你能發佈完整的錯誤/追溯? – rnevius

+0

您是否在settings.py中的已安裝應用程序中包含簡報? –

+0

@joelgoldstick yes我在settings.py – 99darshan

回答

0

只是猜測。嘗試在newsletter之前在admin.pyforms.py之間輸入., G。

from .newsletter.forms import SignUpForm 
+0

我已經嘗試過使用相對導入,但沒有解決問題。 – 99darshan

0

您必須添加myproject文件夾內__init__.py文件。也嘗試像這樣進口

from .models import SignUp