Итак, у меня есть одна страница, которая содержит форму, чтобы пользователь мог отправить данные в базу данных, и вторая страница, которая содержит конкретные поля из этой записи, используя назначенное значение pk.
Мой вопрос заключается в том,Я могу использовать кнопку отправки, чтобы отправить данные в базу данных , а затем немедленно загрузить URL-адрес со значением pk из этой переданной записи, чтобы использовать некоторые поля?Или это нужно сделать в 2 этапа?т.е. 1. вставьте данные в БД, затем 2. загрузите URL с соответствующим pk ..
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
).Кроме того, если я ввожу URL http://127.0.0.1:8000/userregistration/adherence_agreement/6
, он переходит на страницу с определенными полями, извлеченными из этой записи (я вручную ввожу 6 в URL на основе записи в БД).
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 %}