将数据填充到Model Form Django中,并在URL Django中传递id参数

时间:2019-06-24 07:32:10

标签: django django-forms django-views

我正在尝试将数据填充到模型表单中,并使用对象的信息字段加载页面。但是,出现一些错误,我不知道要修复

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')

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_CSOC\env\lib\site- 
packages\django\urls\resolvers.py in _reverse_with_prefix, line 668
Python Executable:  F:\FTEL_CSOC\env\Scripts\python.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

知道为什么我会收到此错误吗? 我试图修复链接建议,但未找到结果页面

1 个答案:

答案 0 :(得分:0)

您需要在id的个人资料URL反转中传递views.py。尝试这样的事情:

{% url 'profile' id=user.id %}