如何将 url 中捕获的值传递给 html 页面?
How to pass value captured in url to the html page?
我不知道如何将值从 url 传递到 UpdateView。然后我需要将该值传递到我的 html 页面。
urls.py
,
对应行
url(r'^poi/edit/(?P<pk>\d+)', POIFormUpdateView.as_view(), name='POIform-edit')
views.py
class POIFormUpdateView(UpdateView):
model = PointOfInterest
fields = ['name', 'vip', 'category', 'place', 'latitude', 'longitude', 'picture', 'website', 'description', 'phone', 'email']
template_name_suffix = '_update_form'
def get(self, request, *args, **kwargs):
print 'On POI get method'
print kwargs
super(POIFormUpdateView, self).get(request, *args, **kwargs)
我在这里尝试重写 get
方法,kwargs
打印预期的字典,但我不知道如何将该字典传递到我的 update_form.html
文件。
update_form.html
,
<form role="form" method="POST" action="/poi/edit/{{ pk }}" class="post-form form-horizontal">{% csrf_token %}
<!-- customizing form -->
{{ form|crispy }}
<!-- End of customization -->
<button type="submit" class="save btn btn-default btn-primary center-block">Save</button>
</form>
我也尝试过从名称中获取 url,例如
<form role="form" method="POST" action="{% url 'POIform-edit' %}"
但这也不行。
我使用<input type='hidden' name='***' value="/poi/edit/{{ pk }}" />
将这个值传递给请求。然后在 def get():
中,您可以使用 myurl=request.POST.get('***')
之类的东西来获取它
将参数对象 ID 添加到 url:
<form role="form" method="POST" action="/poi/edit/{{ object.pk }}/" class="post-form form-horizontal">
ClassBasedView passes model_name as context_object,应该变成:
<form role="form" method="POST" action="/poi/edit/{{ pointofinterest.pk }}/" class="post-form form-horizontal">
我不知道如何将值从 url 传递到 UpdateView。然后我需要将该值传递到我的 html 页面。
urls.py
,
url(r'^poi/edit/(?P<pk>\d+)', POIFormUpdateView.as_view(), name='POIform-edit')
views.py
class POIFormUpdateView(UpdateView):
model = PointOfInterest
fields = ['name', 'vip', 'category', 'place', 'latitude', 'longitude', 'picture', 'website', 'description', 'phone', 'email']
template_name_suffix = '_update_form'
def get(self, request, *args, **kwargs):
print 'On POI get method'
print kwargs
super(POIFormUpdateView, self).get(request, *args, **kwargs)
我在这里尝试重写 get
方法,kwargs
打印预期的字典,但我不知道如何将该字典传递到我的 update_form.html
文件。
update_form.html
,
<form role="form" method="POST" action="/poi/edit/{{ pk }}" class="post-form form-horizontal">{% csrf_token %}
<!-- customizing form -->
{{ form|crispy }}
<!-- End of customization -->
<button type="submit" class="save btn btn-default btn-primary center-block">Save</button>
</form>
我也尝试过从名称中获取 url,例如
<form role="form" method="POST" action="{% url 'POIform-edit' %}"
但这也不行。
我使用<input type='hidden' name='***' value="/poi/edit/{{ pk }}" />
将这个值传递给请求。然后在 def get():
中,您可以使用 myurl=request.POST.get('***')
将参数对象 ID 添加到 url:
<form role="form" method="POST" action="/poi/edit/{{ object.pk }}/" class="post-form form-horizontal">
ClassBasedView passes model_name as context_object,应该变成:
<form role="form" method="POST" action="/poi/edit/{{ pointofinterest.pk }}/" class="post-form form-horizontal">