开发人员。我正在学习这个book之后的django并对django 1.8进行更改
显示的错误是
name 'addPublisher' is not defined
Request Method: GET
Request URL: http://127.0.0.1:8000/add
Django Version: 1.8.7
Exception Type: NameError
Exception Value:
*name 'addPublisher' is not defined*
Exception Location: /home/abhi/Desktop/Trade/Trade/urls.py in <module>, line 26
Python Executable: /usr/bin/python
Python Version: 2.7.11
Python Path:
['/home/abhi/Desktop/Trade',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages']
服务器时间:2016年7月23日星期六18:07:53 +0000
我的views.py是 -
from django.shortcuts import render
from django.db.models import Q
from django.shortcuts import render_to_response
from django.template import Template, Context
from books.models import Book
from books.forms import ContactForm,PublisherForm
from django.core.mail import send_mail
# Create your views here.
def search(request):
query = request.GET.get('q', '')
if query:
qset = (
Q(title__icontains=query) |
Q(authors__first_name__icontains=query) |
Q(authors__last_name__icontains=query)
)
results = Book.objects.filter(qset).distinct()
else:
results = []
return render_to_response("search.html", {
"results": results,
"query": query
})
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
topic = form.clean_data['topic']
message = form.clean_data['message']
sender = form.clean_data.get('sender', 'noreply@example.com')
send_mail(
'Feedback from your site, topic: %s' % topic,
message, sender,
['administrator@example.com']
)
return HttpResponseRedirect('/contact/thanks/')
else:
form = ContactForm()
return render_to_response('contact.html', {'form': form})
def addPublisher(request):
if request == 'POST':
form = PublisherForm(request.POST)
if form.is_valid:
form.save()
return HttpResponseRedirect('add_publisher/thanks/')
else:
form = PublisherForm()
return render_to_response('book/addPublisher.html',{'form':form})
我的urls.py有以下代码 -
from django.conf.urls import include, url
from django.contrib import admin
from views import *
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$',home),
url(r'^search/$',search),
url(r'^contact/$',contact),
url(r'^add/$',addPublisher)
]
答案 0 :(得分:1)
您的urls.py文件可能与您的views.py不在同一个应用程序(文件夹)中。 您收到此错误消息称addPublisher 未定义,因为它实际上不是那里,那里我指的是urls.py所在的应用程序
如果上面的特定views.py位于其他应用中,请将其导入类似
的内容
来自[应用名称] .view导入*
无论如何,向我们展示您项目的布局将有助于我们为您提供更直接,更具体的答案!谢谢。