为Djang中的相关数据创建单独的视图

2024-06-06 02:56:50 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在尝试允许用户使用电子病历软件使用表单创建患者实例,并将创建的实例显示在可通过see patient链接访问的单独页面上

This is code for my views.py

我创建了基于类的视图,该视图通过nesting.html呈现

我同时使用了GETPOST方法来获取无界表单,并将有界表单发布到服务器并保存到数据库

from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from nesting.forms import Identity_Form
from nesting.models import Identity_unique

class Identity_view(TemplateView):

    template_name = 'nesting/nesting.html'

    def get(self, request):

        form = Identity_Form()

        Identities = Identity_unique.objects.filter(user = request.user)
        var = {'form': form, 'Identities': Identities}
        return render(request, self.template_name, var)

    def post(self, request):

        form  = Identity_Form(request.POST or None)

        content = None

        if form.is_valid():

            NIS = form.save(commit = False)
            NIS.user = request.user
            NIS.save()
            content = form.cleaned_data['NIS']

            form = Identity_Form()

            return redirect('nesting:nesting')

        var = {'form': form, 'content': content}

        return render(request,self.template_name, var)

This is the nesting.html document

当前,我的代码的这一部分与用于创建患者实例的表单位于同一页上

      {% block body %}

            {% for Identity in Identities %}

            <div class = "row">

              <div class="col-sm"></div>

              <div class = "col-sm">
                <div class = "card " style = "margin-top: 40px;">
                        <ul class = "list-group list-group-flush">
                          <li class = "list-group-item"><a class = "nav-link" href="#">{{Identity.First_Name}}  {{Identity.Last_Name}} </a></li>
                          <li class = "list-group-item"><small><br>NIS: </small> {{ Identity.NIS }}</li>
                        </ul>
                </div>

              </div>
            <div class="col-sm"></div>
            </div>
                      {% endfor %}


    {% endblock %}

这是模板当前状态的图像:

enter image description here

在上面的图像中,我希望表单下创建的卡片列表呈现到可以通过侧菜单中的see patient导航链接访问的页面。我已经在templates文件夹中创建的子文件夹中创建了HTML模板。我只想确保患者数据在单独的页面上使用模板语言显示

谢谢你


Tags: fromimportselfdivform表单requestvar
1条回答
网友
1楼 · 发布于 2024-06-06 02:56:50

为了在可通过“查看患者”链接访问的单独页面上显示患者的卡片列表,您需要创建另一个视图,在该视图中获取登录用户创建的所有患者

class IdentityListyView(ListView):
    model = Identity_unique
    template_name = 'templates/identity_list.html' # your template to show  patients list

    def get_identities(self):
        identities = self.model.objects.filter(user=request.user)
        return identities

    def get_context_data(self, **kwargs):
        '''
        First create context with the required context_data then update the context with super.
        '''
        context = dict()
        context['Identities'] = self.get_identities()
        context.update(super(IdentityListyView, self).get_context_data(**kwargs))
        return context

您可以访问模板中的Identities以显示患者列表,就像您在nesting/nesting.html中所做的那样

相关问题 更多 >