首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Django:将表单提交的数据拉到后续URL中

Django:将表单提交的数据拉到后续URL中
EN

Stack Overflow用户
提问于 2018-05-17 13:19:25
回答 1查看 178关注 0票数 0

因此,我有一个页面,它包含一个表单,以便用户可以向数据库提交数据;第二个页面,通过使用指定的pk值,包含来自该记录的特定字段。

我的问题是,是否可以使用submit按钮将数据发送到数据库,然后立即从提交的记录中加载带有pk值的以使用某些字段?还是需要分两步完成?即: 1.将数据插入DB,然后2.用相关的pk加载URL。

urls.py

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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})

此时,单击"submit“按钮后,信息将正确保存到DB中,并生成一个基本页面,上面写着”谢谢!“显示(thanks.html)。此外,如果我输入了URL,它会带我到一个页面,其中包含从该记录中提取的特定字段(我根据DB中的一个记录手动在http://127.0.0.1:8000/userregistration/adherence_agreement/6中输入6)。

adherence_agreement.html

代码语言:javascript
运行
复制
{% 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 %}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-17 13:33:05

应该覆盖您的get_success_url类的UserView方法

代码语言:javascript
运行
复制
def get_success_url(self):
    return reverse('adherence_agreement', kwargs={'id': self.object.id})

基于类的高级视图是一个有用的网站,用于探索Django中基于泛型类的视图的可重写方法。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50392498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档