I have a basic task management app named as “weekly_reports” (app name created with django-admin).
I have a model named Reports in models.py as follows;
class Reports(models.Model):
def get_days_of_week(year, week_number):
first_day_of_year = datetime.date(year, 1, 1)
first_week_day = first_day_of_year.weekday()
days_to_add = (week_number - 1) * 7 - first_week_day
first_day_of_week = first_day_of_year + datetime.timedelta(days=days_to_add)
week_days = []
for i in range(7):
day = first_day_of_week + datetime.timedelta(days=i)
week_days.append(day.strftime("%B%d"))
return week_days
year = datetime.datetime.today().year
current_date = datetime.datetime.today()
week_number = current_date.isocalendar()[1]
weeks_days = []
for i in range(week_number - 4, week_number + 5):
days = get_days_of_week(year, i)
weeks_days.append((days[0] + "-" + days[-1], days[0] + "-" + days[-1]))
# All the above varibles and method are used to calculate 4 weeks before and after in a list
week = models.CharField(max_length=30, choices=weeks_days, default=weeks_days[4])
user = models.ForeignKey(User, on_delete=models.CASCADE)
TAG_CHOICES = [
('Bitbucket / Confluence / Jira DesTech', 'Bitbucket / Confluence / Jira DesTech'),
('Jira DesTech', 'Jira DesTech'),
('OpenShift', 'OpenShift'),
('Altyapı', 'Altyapı'),
('Monitoring & Otomasyon', 'Monitoring & Otomasyon'),
('Diğer', 'Diğer'),
]
EPIC_CHOICES = [
('Highlight', 'Highlight'),
('Lowlight', 'Lowlight'),
('Escalation', 'Escalation'),
('Information', 'Information'),
]
tags = models.CharField(max_length=50, choices=TAG_CHOICES)
epic = models.CharField(max_length=20, choices=EPIC_CHOICES)
task = models.TextField(max_length=1000)
status = models.BooleanField(blank=True)
def __str__(self):
return f"""{self.id} - {" ".join(self.task.split(" ")[:3])} - {self.user}"""
def get_absolute_url(self):
return reverse("report.detail", kwargs={"pk": self.pk})
def edit(self):
return reverse("report.update", kwargs={"pk": self.pk})
def remove(self):
return reverse("report.delete", kwargs={"pk": self.pk})
def add(self):
return reverse("report.add")
def list(self):
return reverse("report.list")
In the views.py file;
class ReportsCreateView(LoginRequiredMixin, CreateView):
model = Reports
form_class = ReportsForm
success_url = "/reports"
context_object_name = "object"
def get_initial(self):
return {'user': self.request.user}
I have the CreateView class as above.
I have also forms.py as follows;
class ReportsForm(forms.ModelForm):
class Meta:
model = Reports
fields = ["week", "user", "tags", "epic", "task", "status"]
help_text = {
"week":"Task Week",
"user":"User",
"tags":"Task Tag",
"epic":"Task Epic",
"task":"Write your task definiton briefly",
"status":"Is your task completed?"
}
labels = {
"week":"week",
"user":"user",
"tags":"tags",
"epic":"epic",
"task":"task",
"status":"status"
}
def get_days_of_week(year, week_number):
first_day_of_year = datetime.date(year, 1, 1)
first_week_day = first_day_of_year.weekday()
days_to_add = (week_number - 1) * 7 - first_week_day
first_day_of_week = first_day_of_year + datetime.timedelta(days=days_to_add)
week_days = []
for i in range(7):
day = first_day_of_week + datetime.timedelta(days=i)
week_days.append(day.strftime("%B%d"))
return week_days
year = datetime.datetime.today().year
current_date = datetime.datetime.today()
week_number = current_date.isocalendar()[1]
weeks_days = []
for i in range(week_number - 4, week_number + 5):
days = get_days_of_week(year, i)
weeks_days.append(days[0] + "-" + days[-1])
week = forms.SelectMultiple(choices=weeks_days )
TAG_CHOICES = [
('Bitbucket / Confluence / Jira DesTech', 'Bitbucket / Confluence / Jira DesTech'),
('Jira DesTech', 'Jira DesTech'),
('OpenShift', 'OpenShift'),
('Altyapı', 'Altyapı'),
('Monitoring & Otomasyon', 'Monitoring & Otomasyon'),
('Diğer', 'Diğer'),
]
tags = forms.SelectMultiple(choices=TAG_CHOICES)
EPIC_CHOICES = [
('Highlight', 'Highlight'),
('Lowlight', 'Lowlight'),
('Escalation', 'Escalation'),
('Information', 'Information'),
]
epic = forms.SelectMultiple(choices=EPIC_CHOICES)
task = forms.CharField(max_length=1000, required=True)
status = forms.BooleanField(initial=False)
def __init__(self, *args, **kwargs):
super(ReportsForm, self).__init__(*args, **kwargs)
for i in self.Meta.fields[:-1]:
self.fields[i].widget.attrs['class'] = 'form-control w-75'
self.fields[i].widget.attrs['placeholder'] = i.title()
self.fields[i].label = ''
self.fields[i].help_text = f'<span class="form-text text-muted"><small>{self.Meta.help_text[i].title()}</small></span>'
self.fields["status"].widget.attrs["class"] = "form-text w-25"
self.fields["status"].widget.attrs['placeholder'] = self.Meta.fields[-1].title()
self.fields["status"].label = 'Status'
self.fields["status"].help_text = f'<span class="form-text text-muted"><small>{self.Meta.help_text["status"].title()}</small></span>'
self.fields["user"].disabled = True
In the template for this object I have the below html code;
{% block content %}
<div class="container ">
<div class="card shadow p-3 col-6 d-flex flex-column justify-content-center">
<form method="post" class="form w-100">
{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-primary" type="submit">
<a href="{% url 'report.list' %}" style="color: white; text-decoration: none;">Save</a>
</button>
</form>
</div>
</div>
{% endblock content %}
I can see the form in the browser, everything related to viewing objects and listing works as desired. However, I can not create Reports objects using this setup. Sometimes CreateView creates Reports sometimes does not create.
For debugging, I installed debug_toolbar and enabled “INTERCEPT_REDIRECTS”. When CreateView did not create Reports object, INTERCEPT_REDIRECTS did not intercepted the redirection. When CreateView did create Reports object, I can see the related INSERT script in the SQL tab of debug_toolbar.
I could not be able to further debug. What could be the possible reasons for CreateView failure?