问题描述
我正在制作一个django应用程序,该应用程序能够发送和接收电子邮件.对于django应用程序,我正在为views.py编写代码.运行文件后,我陷入了这个错误.这是我的Django应用程序中的views.py
I was making a django app which would be able to send and receive emails. For the django app I was writing the code for views.pyAfter I ran the file, I got stuck with this error.This is the views.py from my django app
from django.shortcuts import render
from django.contrib import messages
from django.core.mail import send_mail
#from demoapp.form import ContactForm
# Create your views here.
def index(request):
return render(request,'index.html',{'page':'home'})
def contact(request):
try:
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
send_mail(form.cleaned_data)
messages.success(request,'Your message is successfully submitted')
else:
form = ContactForm()
except:
messages.error(request,'contact.html',{'page':'contact','form':form})
def clear(request):
form = ContactForm()
messages.error(request,'Fields Cleared')
return render(request,'contact.html',{'page':'contact','form':form})
Urls.py 该urls.py来自django项目.我尚未为我的应用创建任何urls.py.
Urls.pyThis urls.py is from the django project. I havent created any urls.py for my app.
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from demoapp.views import index,about,contact,clear
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$' , index , name=index),
url(r'^about/$' , about , name=about),
url(r'^contact/$' , contact , name=contact),
url(r'^clear/$' , clear , name=clear),
]
运行python3 manage.py runserver之后,出现以下错误
After I run python3 manage.py runserver I get the following error
Watching for file changes with StatReloader
Performing system checks...
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.6/dist-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 399, in check
messages.extend(check_resolver(pattern))
File "/usr/local/lib/python3.6/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 325, in check
warnings = self._check_pattern_name()
File "/usr/local/lib/python3.6/dist-packages/django/urls/resolvers.py", line 333, in _check_pattern_name
if self.pattern.name is not None and ":" in self.pattern.name:
TypeError: argument of type 'function' is not iterable
推荐答案
您的名称
不是字符串,而是对视图 functions 的引用.您应该改用字符串,例如:
Your name
s are not strings, but references to the view functions. You should use strings instead, like:
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^$' , index , name='index'),
url(r'^about/$' , about , name='about'),
url(r'^contact/$' , contact , name='contact'),
url(r'^clear/$' , clear , name='clear'),
]
因此,我们在这里使用字符串文字 'index'
代替仅引用视图函数的 index
标识符.
So we here use a string literal 'index'
instead of the index
identifier, which just refers to the view function.
这篇关于如何在Python中修复'TypeError:'function'类型的参数不可迭代'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!