我对django和python非常陌生,我正在制作一个表单来接受用户的值并存储在数据库中,我一直遇到ModuleNoFoundError,即使我已经在www.example.com中编写了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条答案
按热度按时间nzkunb0c1#
你导入了错误的模块,你是从
django
导入的,所以不是你自己的。您可以用途:其中 *
app_name
* 是您在其中定义了视图的views.py
的应用程序的名称。注意:URL通常有一个 * 尾随斜杠 *(即
form/
而不是form
)。实际上,默认情况下,**APPEND_SLASH
**设置[Django-doc],默认设置为True
,将首先尝试匹配路径,如果没有找到匹配,并且请求的路径没有斜杠,它将使用附加的斜杠重试。