我正在尝试将数据填充到模型表单中,并使用对象的信息字段加载页面。但是,我得到一些错误,我不知道修复
views.py
def profile(request, id):
user = get_object_or_404(User, id=id)
user_info = get_object_or_404(UserInfor, user_id=user.id)
user_form = UserForm(instance=user)
profile_form = ProfileForm(instance=user_info)
return render(request, 'pages/profile.html', {'user': user_form, 'user_info': profile_form})
urls.py
path('profile/id=<int:id>/', views.profile, name='profile')
简介.html
<form class="form-horizontal" name="form" method="post" action="{% url 'profile' %}">
我得到的错误
NoReverseMatch at /profile/id=2/
Reverse for 'profile' with no arguments not found. 1 pattern(s) tried:
['profile\/id\=(?P<id>[0-9]+)\/$']
Request Method: GET
Request URL: http://127.0.0.1:8000/profile/id%3D2/
Django Version: 2.2.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'profile' with no arguments not found. 1 pattern(s) tried:
['profile\/id\=(?P<id>[0-9]+)\/$']
Exception Location: F:FTEL_CSOCenvlibsite-
packagesdjangourlsresolvers.py in _reverse_with_prefix, line 668
Python Executable: F:FTEL_CSOCenvScriptspython.exe
Python Path:
['F:\FTEL_CSOC\webping',
'F:\FTEL_CSOC\webping',
'F:\FTEL_CSOC\webping',
'C:\Program Files\JetBrains\PyCharm
2019.1.1\helpers\pycharm_display',
'F:\FTEL_CSOC\env\Scripts\python36.zip',
'C:\Users\vuthe\AppData\Local\Programs\Python\Python36\DLLs',
'C:\Users\vuthe\AppData\Local\Programs\Python\Python36\lib',
'C:\Users\vuthe\AppData\Local\Programs\Python\Python36',
'F:\FTEL_CSOC\env',
'F:\FTEL_CSOC\env\lib\site-packages',
'F:\FTEL_CSOC\env\lib\site-packages\setuptools-40.8.0-py3.6.egg',
'C:\Program Files\JetBrains\PyCharm '
'2019.1.1\helpers\pycharm_matplotlib_backend']
Server time: Mon, 24 Jun 2019 07:29:28 +0000
知道为什么我会收到此错误吗?我试图修复链接建议,但找不到结果页面
您需要在
views.py
中的个人资料URL反转中传递id
。尝试这样的事情:
{% url 'profile' id=user.id %}