导入错误 - ImportError:没有名为src.adec.forms的模块

时间:2015-11-14 00:23:45

标签: python django postgresql

我正在尝试修复我的网站,以便当我在表单中输入一些细节时,它会保存在数据库中。如果我应该重新说出这个问题,请告诉我。我是新手。运行python manage.py runserver时,我在控制台上收到以下错误:

unknown-6c-40-08-a3-53-04:src vaijoshi$ python manage.py runserver
Unhandled exception in thread started by <function wrapper at 0x10fbe6500>
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Library/Python/2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/Library/Python/2.7/site-packages/debug_toolbar/settings.py", line 228, in patch_all
    patch_root_urlconf()
  File "/Library/Python/2.7/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 549, in reverse
    app_list = resolver.app_dict[ns]
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 351, in app_dict
    self._populate()
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 284, in _populate
    for pattern in reversed(self.url_patterns):
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 401, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 395, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/vaijoshi/PycharmProjects/adec/src/project/urls.py", line 5, in <module>
    from .views import *
  File "/Users/vaijoshi/PycharmProjects/adec/src/project/views.py", line 6, in <module>
    from src.adec.forms import UserForm
ImportError: No module named src.adec.forms

结构:

Tree updated

项目/ views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render


# Create your views here.
from src.adec.forms import UserForm


def home(request):
    return render(request, "home.html")


def register_professional(request):
    return render(request, "registerprofessional.html")


def register_user(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = UserForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:
            return HttpResponseRedirect('/thanks/')

    # if a GET (or any other method) we'll create a blank form
    else:
        form = UserForm()

    return render(request, 'registeruser.html', {'form': form})


def terms_and_conditions(request):
    return render(request, "termsandconditions.html")


def how_it_works(request):
    return render(request, "howitworks.html")

forms.py

from django import forms

from .models import *


class ProfessionalForm(forms.ModelForm):
    class Meta:
        model = Professional
        fields = '__all__'


class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = '__all__'

Would appreciate any guidance.
Ty

将views.py从projects文件夹导入src文件夹后:

views.py in src folder

它给了我同样的错误:

  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 395, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/vaijoshi/PycharmProjects/adec/src/project/urls.py", line 12, in <module>
    url(r'^termsandconditions/$', terms_and_conditions, name='terms_and_conditions'),
NameError: name 'terms_and_conditions' is not defined

1 个答案:

答案 0 :(得分:0)

直到我所知道的,在python中你只需要导入文件名,因为导入的文件和导入的文件都在同一目录中。如果它们位于同一目录中(我认为它可能是第一个&#34; adec&#34;文件夹),只需&#34;导入表单&#34;。那可能有用......

作为答案发布,因为我在移动电话中并且没有评论&#34;按钮。