0
我正在修復我的網站,以便在窗體中輸入一些細節時將其保存在數據庫中。請讓我知道如果我應該重新提出這個問題,我是新手。當運行的python manage.py runserver命令我得到我的控制檯上下面的錯誤:導入錯誤 - ImportError:沒有名爲src.adec.forms的模塊
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
結構:
項目/ 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")
形式。 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後文件夾複製到src文件夾:
它給了我同樣的錯誤:
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
我嘗試了你所建議的,但仍然得到如上所示的導入錯誤。不確定該做什麼。有人有主意嗎? – vtj808
我真的認爲是與目錄有關的東西...嘗試將類導入到其外部的文件,以查看是否存在問題。順便說一句,你的文本編輯器是什麼(我喜歡它)。 – TheShadlest
感謝,它是PyCharm :)所以要直言不諱,我對Python有點新,所以不太確定你在暗示我嘗試了什麼。你可以把它拼出來給我一點嗎?我還添加了更新的樹。該文件目前位於項目中 - > views.py – vtj808