将使用 django 表单并发布数据的用户

问题描述 投票:0回答:1

我创建了一个具有 CRUD 功能的 django Web 应用程序,我希望能够看到已在表单上发布的用户(及其姓名),并查看他们发布的数据的完整列表以及数据旁边的姓名。我不知道如何编码。

我的型号代码是:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Agent(models.Model):

Role = [
    ('Manager','Manager'),
    ('Agent','Agent'),
]

QA_Agent = models.OneToOneField(User, on_delete=models.CASCADE, choices=Role)

def __str__(self):
    return self.QA_Agent.username

QA_Outcome = [
('FALSE','FALSE'),
('TRUE','TRUE'),
]

KPA_Outcome = [
   ('Product','Product'),
   ('Pass','Pass'),
   ('Compliance','Compliance'),
   ('Data Capturing','Data Capturing'),
   ('TCF','TCF'),
 ]

HIV_Test = [
    ('YES','YES'),
    ('NO','NO'),
]

AVS = [
    ('PASS', 'PASS'),
    ('FAIL', 'FAIL'),
    ('UNVERIFIED', 'UNVERIFIED'),

]

StartDate = [
    ('1st January','1st January'),
    ('1st February','1st February'),
    ('1st March','1st March'),
    ('1st April','1st April'),
    ('1st May','1st May'),
    ('1st June','1st June'),
    ('1st July','1st July'),
    ('1st August','1st August'),
    ('1st September','1st September'),
    ('1st October','1st October'),
    ('1st November','1st November'),
    ('1st December','1st December'),

]

# Create your models here.
class QA_Models(models.Model):
    Policy_Number = models.CharField(max_length=12)
    Case_Number = models.CharField(max_length=7)
    AVS_Check = models.CharField(choices=AVS, max_length=12)
    Caller_ID = models.CharField(max_length=50)
    Call_duration = models.CharField(max_length=10)
    Start_date = models.CharField(choices=StartDate, max_length=20)
    Premium = models.DecimalField(max_digits=6, decimal_places=2)
    Debit_date = models.CharField(max_length=10)
    Cover_amount = models.CharField(max_length=10)
    QA_Correct = models.CharField(choices=QA_Outcome, max_length=5)
    KPA_1 = models.CharField(choices=KPA_Outcome, max_length=15, null=True)
    KPA_2 = models.CharField(choices=KPA_Outcome, max_length=15, null=True)
    KPA_3 = models.CharField(choices=KPA_Outcome, max_length=15, null=True)
    HIV_Required = models.CharField(choices=HIV_Test,  max_length=3)
    Comment = models.TextField(max_length=200)
    
    def __str__(self):
        return self.Case_Number

我的view.py中的post方法是

from django.shortcuts import render, redirect
from . models import QA_Models, Agent
from django.contrib.auth.models import User
from . form import QA_Forms
from . decorations import allowed_user

# Create your views here.
@allowed_user(allowed_roles=['Manager','Agent'])
def Home(request):
    TheData = QA_Models.objects.all()
    return render(request, 'Home.html',
                  {'TheData':TheData})   

@allowed_user(allowed_roles=['Manager','Agent'])
def MainPage(request):
    TheForm = QA_Forms
    TheUser = request.user
    if request.method == 'POST':
        TheForm = QA_Forms(request.POST, request.user)
        if TheForm.is_valid():
            #TheForm.user = request.user
            TheForm.save()          
    return render(request, 'MainPage.html', {'TheForm':TheForm,
                                             'TheUser':TheUser})

我的 HTML 是:

    {% extends "Layout.html" %}

{% block content %}

<h1>Home </h1>
 <div class="container">
        <br>

        <table class="table table-dark table-hover">
            {% for Information in TheData %}
            <tr>
                <td>
                    {{Information.Policy_Number}}<br>
                </td>
                <td>
                   <a href={% url 'DataInformation' Information.id  %}>
                      {{Information.Case_Number}}<br>
                   </a>
                </td>
                <td>
                    {{Information.QA_Correct}}<br>
                </td>


            </tr>
            {% endfor %}

        </table>
 </div>
{% endblock %}

请让我向我展示如何在登录我的网络应用程序后使用在表单上发布的用户。

python django authentication
1个回答
0
投票

您正在寻找的内容似乎需要创建一个单独的模型,该模型与您的

QA_Models
有关系。请随意从原始
QA_Models
模型中删除评论字段。

让我们通过创建一个

Comment
模型来看看这在实践中会是什么样子。

模型示例

class Comment(models.Model):
    post = models.ForeignKey(QA_Models, on_delete=models.CASCADE, null=True, blank=True,related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
    content = models.TextField(max_length=400)
    timestamp = models.DateTimeField(auto_now_add=True)

def __str__(self):
    return self.content

这将允许您循环遍历所有评论及其各自的内容和用户,就像这样

模板示例

    <h2>Comments</h2>
    <ul>
    {% for comment in Information.comments.all %}
        <li>{{ comment.content }} - {{ comment.user.username }}</li>
    {% endfor %}
    </ul>
© www.soinside.com 2019 - 2024. All rights reserved.