提问人:Sneha_R 提问时间:10/15/2023 更新时间:10/15/2023 访问量:27
即使我已经定义了函数,我也收到“AttributeError:模块'django.views'没有属性'my_form'”
I'm getting a "AttributeError: module 'django.views' has no attribute 'my_form'" even though I have defined the function
问:
我对 django 和 python 非常陌生,我正在制作一个表单来接受来自用户的值以存储在数据库中,并且我一直遇到 ModuleNoFoundError,即使我已经用 views.py 编写了我正在导入的模块。我正在创建自己的模块和表单的html,根本不使用django表单格式。
这是我 views.py:
`from django.shortcuts import render
# Create your views here.
from .models import My_Model
from .forms import MyForm
def my_form(request):
if request.method == "POST":
form = MyForm(request.POST)
if form.is_valid():
form.save()
else:
form = MyForm()
return render(request, 'form.html', {'form': form})
这是我 urls.py:
`from django import urls
from django import views
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
urls(r'form', views.my_form , name='form')
]
`
这是 models.py:
`from django.db import models
# Create your models here.
class My_Model(models.Model):
firstname = models.CharField(max_length=100)
lastname = models.CharField(max_length=100)
PRN = models.CharField(max_length=8)
Phone = models.IntegerField(max_length=10)
Email = models.EmailField()
Department = models.CharField(max_length=50)
S_Year = models.CharField(max_length=2)
def __str__(self):
return self.name`
这是 forms.py:
`from django import forms
from .models import My_Model
class MyForm(forms.ModelForm):
firstname = forms.CharField(widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "firstname"
}))
lastname = forms.CharField(widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "lastname"
}))
prn = forms.CharField(widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "prn"
}))
phone = forms.IntegerField(widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "Phone Number"
}))
email = forms.EmailField(widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "Email"
}))
department = forms.CharField(widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "Department"
}))
S_Year = forms.CharField(widget=forms.TextInput(attrs={
"class": "form-control",
"placeholder": "Year of Study"
}))
class Meta:
model = My_Model
fields = [
'firstname', 'lastname','prn', 'phone', 'email', 'department', 'S_Year'
]
`
I keep getting this error:
`Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
self.run()
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 975, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\commands\runserver.py", line 133, in inner_run
self.check(display_num_errors=True)
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 485, in check
all_issues = checks.run_checks(
^^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
return check_method()
^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py", line 494, in check
for pattern in self.url_patterns:
^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py", line 715, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\urls\resolvers.py", line 708, in urlconf_module
return import_module(self.urlconf_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\jyoth\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\jyoth\OneDrive\Documents\sneha proj\GDP\Pinnacle\pinnacle_project\pinnacle_project\urls.py", line 24, in <module>
urls(r'form', views.my_form , name='form')
^^^^^^^^^^^^^
AttributeError: module 'django.views' has no attribute 'my_form'`
请帮我解决这个问题。
我尝试了不同的解决方案,例如直接添加视图和 url 的路径,尝试直接从视图文件导入函数,但它们仍然向我显示错误,例如:
`from .views import my_form
ModuleNotFoundError: No module named 'pinnacle_project.views'`
或
`urls(r'form', views.my_form , name='form')
^^^^^^^^^^^^^
AttributeError: module 'django.views' has no attribute 'my_form'
答:
1赞
willeM_ Van Onsem
10/15/2023
#1
您导入了错误的模块,您是从 导入的,所以不是您自己的。您可以使用:django
from django.contrib import admin
from django.urls import path
from app_name.views import my_form
urlpatterns = [
path('admin/', admin.site.urls),
path('form', my_form, name='form'),
]
其中app_name
是应用的名称,您可以在其中定义该视图。views.py
注意:URL 通常有一个尾部斜杠(即 而不是 )。实际上,默认情况下,
APPEND_SLASH
设置 [Django-doc](默认设置为 )将首先尝试匹配路径,如果未找到匹配项,并且请求的路径没有斜杠,它将重试并附加斜杠。form/
form
True
评论