I'm just starting out with Django and I have a problem where, when I added a field to my model (JobPost which inherits from models.Model) and it migrated successfully I can see and interact with the new field when I create a job post, but not when I view the JobPost (which is displayed using crispy forms using the {{ form|crispy }} tag.
I have added the field name to my fields = [''] in my 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 %}
So when I view it in my browser, I see the "clearance_required" field when I create a job post. But when I just view the job post, it only shows the title and description, not the new clearance_required field. I don't know how I can get it to display.
Here's a picture of the issue: Notice how the clearance field is missing in the second picture
Do you actually output in object.clearance_required
in jobpost_detail.html
, because I cannot see it?
And by the way, if you output it, you should use object.get_clearance_required_display
, because it is a field with choices .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.