gpt4 book ai didi

python - 错误: 'NameError: name ' terms_and_conditions' is not defined' when it is

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:10 24 4
gpt4 key购买 nike

我只是在寻找 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中定义的(至少我认为我正在做的事情)

View .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 从项目文件夹移动到 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:

tree

请帮忙

根据以下评论进行更新:

我现在已经完成了:

从 adec.views 导入 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

最佳答案

确保该名称已导入 urls.py 中:

from adec.views import terms_and_conditions

或者,使用字符串代替:

url(r'^termsandconditions/$', 'adec.views.terms_and_conditions',
name='terms_and_conditions'),

关于python - 错误: 'NameError: name ' terms_and_conditions' is not defined' when it is,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33720024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com