所以我有一个页面包含一个表单,以便用户可以使用指定的pk值向数据库提交数据,第二个页面包含该记录中的特定字段。
我的问题是,我是否可以使用提交按钮将数据发布到数据库,然后立即使用该提交记录中的pk值加载URL 以使用某些字段?或者这需要分两步完成?即1.将数据插入DB,然后2.使用相关的pk加载URL ..
urls.py
:
from django.urls import path
from django.conf.urls import url, include
from django.views.generic import TemplateView
from .views import *
from . import views
urlpatterns = [
url(r'^$', UserView.as_view(), name='user_new.html'),
# form temporarily directs here after saving the data to the DB
url(r'^thanks/$', TemplateView.as_view(template_name='thanks.html'), name='thanks'),
# I would like it to go here instead
path('adherence_agreement/<int:id>', views.adherence_agreement, name='adherence_agreement'),
]
views.py
:
from django.views.generic.edit import FormView,CreateView
class UserView(CreateView):
template_name = 'user_new.html'
form_class = UserForm
success_url = 'thanks/'
def post(self, request, *args, **kwargs):
self.object = None
if request.method == 'POST':
form = UserForm(data=request.POST)
if form.is_valid():
form.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
else:
form = UserForm()
def adherence_agreement(request, id):
item = get_object_or_404(UserInfo, pk=id)
return render(request, 'adherence_agreement.html', {'item': item})
目前,点击“提交”按钮后,信息会正确保存到数据库中,并显示“谢谢!”的基本页面。显示(thanks.html
)。此外,如果我输入网址http://127.0.0.1:8000/userregistration/adherence_agreement/6
,它会将我带到从该记录中提取某些字段的页面(我根据数据库中的记录手动输入URL中的6)。
adherence_agreement.html
:
{% extends "base.html" %}
{% load static %}
{% block content %}
<div class="container" id="printable">
<br>
<img src="{% static 'RDConnect_300dpi_RGB.jpg' %}" alt="" style="
display: block;
margin-left: auto;
margin-right: auto;
width: 60%;
"/>
<br>
<hr>
<h3>Adherence agreement for authorized access to data and biospecimens in the RD-Connect Genome-Phenome Analysis Platform (RDC-GPAP)<sup><a href="#fn1" id="ref1">1</a></sup><small class="text-muted"> to be used together with the <span class="text-info">Code of Conduct</span> for integrated user access to RDC-GPAP for health-related information and human biological samples.</small></h3>
<hr>
<h4>Effective as of 9<sup>th</sup> November 2015</h4>
<p>Communication to Mats G. Hansson, Centre for Research Ethics & Bioethics at Uppsala University: mats.hansson@crb.uu.se</p>
<hr>
<p>I, <span class="text-success">{{ item.first_name }} {{ item.last_name }}</span>, acting on behalf of the <span class="text-success">{{ item.department }}</span>, and holding the position <span class="text-success">{{ item.job_title }}</span>, herewith declare:</p>
<ol type="1">
<li>I have the authority to represent and engage the <span class="text-success">{{ item.department }}</span> for the purpose of the project, <span class="text-success">[project name]</span>. For this project we ask for access to data and/or biospecimens in accordance with our research interests as set out in our completed <span class="text-info">User Verification Form</span> (previous page).</li>
<br>
<li>The <span class="text-success">{{ item.department }}</span> agrees to fully endorse and adhere to the <span class="text-info">Code of Conduct</span> for integrated user access to the RDC-GPAP for health-related information and human biological samples, version 2, 3rd November 2017. It shall apply to all data and biospecimen processing activities carried out within the project <span class="text-success">[project name]</span>. The personal data protection framework is thus in part formalized through this Code.</li>
<br>
<li>The <span class="text-success">{{ item.department }}</span> will ensure the implementation of all measures required by the provisions of this Code.</li>
<br>
<li>The <span class="text-success">{{ item.department }}</span> will ensure compliance with this Code by all staff and personnel working within the project on behalf of the <span class="text-success">{{ item.department }}</span>.</li>
<br>
<li>In addition to the rules laid out by the Code, the following project specific rules shall apply:</li>
<br>
<ol type="a">
<li>There will be no attempt to try to identify or contact data or donor subjects.</li>
<li>Accessed data and biospecimens will not be redistributed.</li>
<li>Access codes and user logins are specific to the identified user and are strictly non-transferable.</li>
<li>Accessed datasets will be destroyed once they are no longer used.</li>
<li>Biospecimens will be handled in accordance with specifications in a Material Transfer Agreement.</li>
<li>Publications using any form of data accessed through the RDC-GPAP will contain an acknowledgment and a reference of the RDC-GPAP as appropriate.
Where a publication makes use of individual-level datasets and enriched phenotypic data, authors should consider whether the intellectual contribution of the original contributors of the datasets qualifies for authorship positions in line with standard publication practice.</li>
<li>Results of analyses of accessed data and biospecimens will be returned to the platform. Published work will also be sent to RD-Connect.</li>
<li>In case of misconduct access to the platform will be withdrawn.</li>
</ol>
</ol>
<hr>
<p>Signed on behalf of the <span class="text-success">{{ item.department }}</span> on <span class="text-success">{% now "jS F Y" %}</span> by <span class="text-success">{{ item.first_name }} {{ item.last_name }}</span>:</p>
<br>
<br>
<p>…………………………………………………</p>
<p>Signature</p>
<hr>
<sup id="fn1">1. This adherence agreement is based on the Personal Data Directive, Directive95/46/EC, implying that all personal data shall be handled in accordance with Secrecy law.<a href="#ref1" title="Jump back to footnote 1 in the text.">↩</a></sup>
<br>
<br>
</div>
<div class="container">
<input type="button" class="save btn btn-success btn-lg" onclick="printDiv('printable')" value="Print" />
<!-- <button id="printButton" class="save btn btn-success btn-lg">Print</button> -->
<br>
<br>
</div>
<script>
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
{% endblock %}
答案 0 :(得分:2)
您应该覆盖get_success_url
类
UserView
方法
def get_success_url(self):
return reverse('adherence_agreement', kwargs={'id': self.object.id})
Classy Class Based Views是一个有用的网站,用于探索Django中基于泛型类的视图的可覆盖方法