I'm having trouble making the data from a form be sent to a new form in my application. I want to do the following: After the user clicks a button within a record, that record data be copied to another empty form.
我无法将表单中的数据发送到我的应用程序中的新表单。我想执行以下操作:在用户单击记录中的按钮后,该记录数据将被复制到另一个空表单。
No need to store these values permanently or for a long time. Only in the transition from one form to another with populated fields blank form.
无需永久或长期存储这些值。仅在从一个表单到另一个表单的过渡中,填充字段为空白表单。
Basically a Ctrl + C - Ctrl + V form, but without saving the record after copying ...
基本上是一个Ctrl + C - Ctrl + V形式,但复制后没有保存记录...
I've done something like working with URL parameters, but would like something cleaner. I need to copy values including the inlines.
我做过像URL参数一样的工作,但想要更清洁。我需要复制包括内联的值。
I thought of storing the data in the session, and then retrieve them at the target form. However, I do not know if it's the best way to implement it. If so, how can I work with the session in Django?
我想过将数据存储在会话中,然后在目标表单中检索它们。但是,我不知道这是否是实施它的最佳方式。如果是这样,我如何在Django中使用会话?
This is the code I have so far in my admin.py file:
这是我目前在admin.py文件中的代码:
def response_change(self, request, obj):
# name of the custom button
if '_copy_and_paste' in request.POST:
# getting the data and storing the session
request.session['field_1'] = obj.field_1
request.session['field_2'] = obj.field_2
return super(MyClassAdmin, self).response_change(request, obj)
I'm using the Django admin (version 1.8).
我正在使用Django管理员(版本1.8)。
1 个解决方案
#1
There is no need to store this information in session, you can pass the request object back to your template page.
无需在会话中存储此信息,您可以将请求对象传递回模板页面。
In your settings.py file, make sure context_processors.request is inside the context_processors:
在settings.py文件中,确保context_processors.request位于context_processors中:
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...,
'django.template.context_processors.request', #<- make sure this line is here
...,
],
},
},
]
Then, in your views.py
然后,在views.py中
from django.shortcuts import render
def response_change(request):
if '_copy_and_paste' in request.POST:
return render(request, 'your_template.html')
In your template, to access the data stored in request:
在您的模板中,访问存储在请求中的数据:
<input type="text" name="field1" value="{% if request.POST.field1 %}{{ request.POST.field1}}{% endif %}">
#1
There is no need to store this information in session, you can pass the request object back to your template page.
无需在会话中存储此信息,您可以将请求对象传递回模板页面。
In your settings.py file, make sure context_processors.request is inside the context_processors:
在settings.py文件中,确保context_processors.request位于context_processors中:
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...,
'django.template.context_processors.request', #<- make sure this line is here
...,
],
},
},
]
Then, in your views.py
然后,在views.py中
from django.shortcuts import render
def response_change(request):
if '_copy_and_paste' in request.POST:
return render(request, 'your_template.html')
In your template, to access the data stored in request:
在您的模板中,访问存储在请求中的数据:
<input type="text" name="field1" value="{% if request.POST.field1 %}{{ request.POST.field1}}{% endif %}">