Django Field 出现在一个视图中,但没有出现在另一个视图中

Django Field is showing up in one view but not the other

我刚开始使用 Django,我遇到了一个问题,当我向我的模型(继承自 models.Model 的 JobPost)添加一个字段并且它成功迁移时,我可以当我创建工作时看到新字段并与新字段交互 post,但当我查看 JobPost(使用 {{ form|crispy }} 标签使用脆皮形式显示。

我已经在我的 views.py

字段中添加了字段名称 = ['']

models.py

class JobPost(models.Model):
#constants
CLEARANCE_LEVELS=[(...),]

author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
content = models.TextField()
clearance_required = models.CharField(max_length=50, choices=CLEARANCE_LEVELS, default='None')
date_posted = models.DateTimeField(default=timezone.now)

views.py

class JobDetailView(DetailView):
    model = JobPost

class JobCreateView(LoginRequiredMixin, CreateView):
    model = JobPost
    fields = ['title', 'content', 'clearance_required']
    # form_valid checks if the user is logged in
    def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

jobpost_form.html

{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
    <form method="POST">
        {% csrf_token %} <!--Used to prevent some XSS Attacks (Cross-site request forgery) -->
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">Create Job Post</legend>
            {{ form|crispy }}
        </fieldset>
        <div class="form-group">
            <button class="btn btn-outline-info" type="submit">Submit</button>
        </div>
    </form>
</div>
{% endblock content %}

jobpost_detail.html

{% block content %}
<article class="media content-section">
    <img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
    <div class="media-body">
        <div class="mb-2 article-metadata">
            <a class="mr-1" href="{% url 'profile' %}">{{ object.author }}</a> <!-- BROKEN LINK: SIMPLY TAKES TO CURRENTLY LOGGED IN USER-->
            <small class="text-muted">{{ object.date_posted|date:"F d, Y" }}</small>
            {% if object.author != user %}
                <a class="btn btn-secondary btn-sm float-right" href="{% url 'user-jobs' object.author.username %}">See all jobs the user applied to</a>
            {% endif %}
            <div>
                {% if object.author == user %}
                <a class="btn btn-secondary btn-sm mt-1 mb-1 float-right" href="{% url 'user-jobs' object.author.username %}">See all jobs the user applied to</a>
                <a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'job-update' object.id %}">Update Job Posting</a>
                <a class="btn btn-danger btn-sm mt-1 mb-1" href="{% url 'job-delete' object.id %}">Delete</a>
                {% endif %}
            </div>
        </div>
        <div class="mt-1">
            <h2 class="article-title">{{ object.title }}</h2>
            <p class="article-content">{{ object.content }}</p>
        </div>
    </div>
</article>
{% endblock content %}

因此,当我在浏览器中查看它时,我会在创建职位 post 时看到 "clearance_required" 字段。但是当我只查看工作 post 时,它只显示标题和描述,而不是新的 clearance_required 字段。我不知道如何让它显示。

这是问题的图片: 注意第二张图片中的间隙字段是如何丢失的

你真的在object.clearance_required中输出jobpost_detail.html,因为我看不到吗?

顺便说一句,如果你输出它,你应该使用object.get_clearance_required_displaybecause it is a field with choices