我只是在寻找python / django的方法。我正处于解决另一个问题的麻烦中,在尝试各种事情的过程中我现在在控制台中看到了这个错误:
__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
我有点困惑,因为这些信息是在我的views.py中定义的(至少我认为我在做什么)
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")
def search_results(request):
return render(request, "searchresults.html")
def profile(request):
return render(request, "profile.html")
项目文件夹中的URL.py(注意我被建议将上面的views.py从projects文件夹移动到src文件夹,以解决我正在解决的另一个问题。他们是否需要在同一个文件夹中?)
import...
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/docs/', include('django.contrib.admindocs.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^termsandconditions/$', terms_and_conditions, name='terms_and_conditions'),
url(r'^how-it-works/$', how_it_works, name='how_it_works'),
url(r'^$', home, name='home'),
url(r'^registerprofessional/$', register_professional, name='register_professional'),
url(r'^registeruser/$', register_user, name='register_user'),
url(r'^searchresults/$', search_results, name='search_results'),
url(r'^profile/$', profile, name='profile'),
]
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我的树结构atm:
请帮助
根据以下评论进行更新:
我现在已经完成了:
来自adec.views import terms_and_conditions
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/docs/', include('django.contrib.admindocs.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^termsandconditions/$', terms_and_conditions, name='terms_and_conditions'),
url(r'^how-it-works/$', how_it_works, name='how_it_works'),
url(r'^$', home, name='home'),
url(r'^registerprofessional/$', register_professional, name='register_professional'),
url(r'^registeruser/$', register_user, name='register_user'),
url(r'^searchresults/$', search_results, name='search_results'),
url(r'^profile/$', profile, name='profile'),
]
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这让我回到原来面对的错误:
__import__(name)
File "/Users/vaijoshi/PycharmProjects/adec/src/project/urls.py", line 1, in <module>
from adec.views import terms_and_conditions
File "/Users/vaijoshi/PycharmProjects/adec/src/adec/views.py", line 5, in <module>
from src.adec.forms import UserForm
ImportError: No module named src.adec.forms
答案 0 :(得分:1)
确保在urls.py
:
from adec.views import terms_and_conditions
或者,请改用字符串:
url(r'^termsandconditions/$', 'adec.views.terms_and_conditions',
name='terms_and_conditions'),