简体   繁体   中英

Import Error in django forms

My django project file structure

  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

code in models.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

code in 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)

code in 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', ]

When I hit ctrl+B in sublime text I get an import error saying no module newsletter.models in form.py

Traceback for 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]

Traceback in 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]

Just guess. Try add . before newsletter imports in admin.py and forms.py , eg

from .newsletter.forms import SignUpForm

You have to add __init__.py file inside myproject folder. Also try import like this

from .models import SignUp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM