Django - Cannot get model to display in template - python

I'm creating an availability app for Volunteer Firefighters and cannot get one of the model fields to display in the template:
models.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Team(models.Model):
name = models.CharField('Name', max_length = 200)
def __str__(self):
return self.name
class Firefighter(models.Model):
RANKS = (
('CFO', 'Chief Fire Officer'),
('DCFO','Deputy Chief Fire Officer'),
('SSO', 'Senior Station Officer'),
('SO', 'Station Officer'),
('SFF', 'Senior Firefighter'),
('QFF', 'Qualified Firefighter'),
('FF', 'Firefighter'),
('RFF', 'Recruit Firefighter'),
('OS', 'Operational Support'),
)
STATUS_OPTIONS = (
('AV', 'Available'),
('OD', 'On Duty'),
('UN', 'Unavailable'),
('LV', 'Leave'),
)
rank = models.CharField("Rank", max_length = 50 , choices=RANKS, default='RFF')
first_name = models.CharField("First Name", max_length = 200)
last_name = models.CharField("Last Name", max_length = 200)
start_date = models.DateField(name="Start Date")
status = models.CharField("Status", max_length = 20 , choices=STATUS_OPTIONS, default='Available')
driver = models.BooleanField('Driver', default=False)
officer = models.BooleanField('Officer Qualified', default=False)
teams = models.ManyToManyField(Team)
def __str__(self):
return self.first_name + ' ' + self.last_name
class Meta:
verbose_name = "Firefighter"
verbose_name_plural = "Firefighters"
views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.forms import ModelForm
from .models import Firefighter
from django.http import HttpResponseRedirect
# Create your views here.
#login_required(login_url='/login/')
def status(request):
firefighters = Firefighter.objects.all()
context = {
'firefighters': firefighters,
'availableCount': firefighters.filter(status='AV').count() + firefighters.filter(status='OD').count(),
'leaveCount': firefighters.filter(status='LV').count(),
'unAvCount': firefighters.filter(status='UN').count()
}
return render(request, 'status.html', context)
#login_required(login_url='/login/')
def details(request, id):
class ChangeStatus(ModelForm):
class Meta:
model = Firefighter
fields = ['status']
form = ChangeStatus()
firefighter = Firefighter.objects.get(id=id)
if request.method == 'POST':
form = ChangeStatus(request.POST, instance=firefighter)
if form.is_valid():
form.save()
return HttpResponseRedirect('/status/')
else:
form = ChangeStatus()
context = {
'firefighter': firefighter,
'form': form
}
return render(request, 'details.html', context)
def members(request):
firefighters = Firefighter.objects.all().order_by('rank')
context = {
'firefighters': firefighters,
}
return render(request, 'members.html', context)
and the members.html that won't render correctly:
{% extends "base.html" %}
{% block content %}
<ul>
{% for firefighter in firefighters %}
<li> {{firefighter.rank}} {{firefighter.first_name}} {{firefighter.last_name}} {{firefighter.start_date}}</li>
{% endfor %}
</ul>
{% endblock content %}
The members.html will correctly render firefighter.first_name and firefighter.last_name but nothing displays for firefighter.start_date even though each firefighter has a specified start date in the database.
Any ideas as to why?
Bonus question: Is there any way for me to sort the ranks as they are listed in the model when querying the database?

Where you define the field for the start_date you have:
start_date = models.DateField(name="Start Date")
It should be:
start_date = models.DateField("Start Date")
The first arg in Date_Field is verbose_name which will automatically set the name with a more acceptable snake_case version of the name.

As far as your ordering, try setting variables for your choices with a value that will sort.
CFO = ‘A’
DCFO = ‘B’
...
RANK_CHOICES = (
(CFO, ‘Chief Fire Officer’),
(DCFO, ‘Deputy Chief Fire Officer’)
(...)
)
If you always wanted query results by rank:
class Meta:
ordering = [‘rank’]
If not, use the current query you have.
As mentioned in Zev’s answer “name=‘label’” is not valid for any field. It is verbose_name=‘label. Depending on the field you can put the label as the first argument in quotes, but other fields you have to use verbose_name.

Related

Django AssertionError when testing Create View

I'm running some tests for my app 'ads', but when I try to test the CreateView it fails with the following message:
AssertionError: 'just a test' != 'New title'
Here's the test:
class AdTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(
username='test_user',
email='test#email.com',
password='secret'
)
self.ad = Ad.objects.create(
title='just a test',
text='Ehy',
owner=self.user
)
def test_ad_create_view(self):
response = self.client.post(reverse('ads:ad_create'), {
'title': 'New title',
'text': 'New text',
'owner': self.user.id,
})
self.assertEqual(response.status_code, 302)
self.assertEqual(Ad.objects.last().title, 'New title')
self.assertEqual(Ad.objects.last().text, 'New text')
So it could be that the test fails in creating a new ad, and then it compares the fields with the first ad in the setUp method.
I upload the rest of the code if it can help:
urls.py
from django.urls import path, reverse_lazy
from . import views
app_name='ads'
urlpatterns = [
path('', views.AdListView.as_view(), name='all'),
path('ad/<int:pk>', views.AdDetailView.as_view(), name='ad_detail'),
path('ad/create',
views.AdCreateView.as_view(success_url=reverse_lazy('ads:all')), name='ad_create'),
...
]
models.py
class Ad(models.Model) :
title = models.CharField(
max_length=200,
validators=[MinLengthValidator(2, "Title must be greater than 2 characters")]
)
price = models.DecimalField(max_digits=7, decimal_places=2, null=True)
text = models.TextField()
"""We use AUTH_USER_MODEL (which has a default value if it is not specified in settings.py) to create a Foreign Key relationship between the Ad model
and a django built-in User model"""
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
comments = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Comment', related_name='comments_owned')
picture = models.BinaryField(null=True, editable=True)
tags = TaggableManager(blank=True)
content_type = models.CharField(max_length=256, null=True, help_text='The MIMEType of the file')
favorites = models.ManyToManyField(settings.AUTH_USER_MODEL, through='Fav', related_name='favorite_ads')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
views.py
class AdCreateView(LoginRequiredMixin, View):
template_name = 'ads/ad_form.html'
success_url = reverse_lazy('ads:all')
def get(self, request, pk=None):
form = CreateForm()
ctx = {'form': form}
return render(request, self.template_name, ctx)
# Pull data
def post(self, request, pk=None):
form = CreateForm(request.POST, request.FILES or None)
if not form.is_valid():
ctx = {'form': form}
return render(request, self.template_name, ctx)
pic = form.save(commit=False)
pic.owner = self.request.user
pic.save()
form.save_m2m()
return redirect(self.success_url)
forms.py (the view uses it especially to check and save the image)
class CreateForm(forms.ModelForm):
max_upload_limit = 2 * 1024 * 1024
max_upload_limit_text = naturalsize(max_upload_limit)
# Call this 'picture' so it gets copied from the form to the in-memory model
# It will not be the "bytes", it will be the "InMemoryUploadedFile"
# because we need to pull out things like content_type
picture = forms.FileField(required=False, label='File to Upload <= ' + max_upload_limit_text)
upload_field_name = 'picture'
class Meta:
model = Ad
fields = ['title', 'text', 'price', 'picture', 'tags']
# Check if the size of the picture is less than the one specified (see above).
def clean(self):
cleaned_data = super().clean()
pic = cleaned_data.get('picture')
if pic is None:
return
if len(pic) > self.max_upload_limit:
self.add_error('picture', "File must be < " + self.max_upload_limit_text + " bytes")
# Convert uploaded File object to a picture
def save(self, commit=True):
instance = super(CreateForm, self).save(commit=False)
# We only need to adjust picture if it is a freshly uploaded file
f = instance.picture # Make a copy
if isinstance(f, InMemoryUploadedFile): # Extract data from the form to the model
bytearr = f.read()
instance.content_type = f.content_type
instance.picture = bytearr # Overwrite with the actual image data
if commit:
instance.save()
self.save_m2m()
return instance
I hope it is useful, thanks in advance!
According to Django Doc
Create View:
A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object.
This is not a Valid way to do create View based on my experience. Check the doc Doc here.
if i understand what you are talking about You want to submit Ad Model using Create View, if you want to submit it in form You can something like this:
from django.views.generic import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy,
class PostCreativeView(LoginRequiredMixin, CreateView):
model = #Your Model
fields = [#Fields of the model You want to submit]
template_name = #html template you want to submit the form
success_url = reverse_lazy(#url for redirected user when the form is submitted)
def form_valid(self, form):
form.instance.user = self.request.user
return super (PostCreativeView, self).form_valid(form)
in the form template you can add:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
for styling you can follow this: Answer

Not able to save data using Django form

I stack. I try many thinks. I want to put 2 forms in my mainpage. Models, forms in index.html, ModelForm, save(), urls.. I think everything ok. But submit button do nothing.
#models.py
from django.db import models
class Iletisim(models.Model):
DURUM = [
('KT', 'Keşif Talebi'),
('AB', 'Arıza Bildirimi'),
('IL', 'İletişimden'),
]
SINIF = [
('Konut', (
('SI', 'Site '),
('DA', 'Apartman Dairesi'),
('VI', 'Yazlık/Bağ/Villa'),
)
),
('İşyeri', (
('OF', 'Ofis/Büro/Dükkan'),
('FA', 'Fabrika/Şantiye/Otel/Okul'),
)
),
('DG', 'Diğer'),
]
name = models.CharField(max_length=100, verbose_name="Ad/Soyad")
phone = models.CharField(max_length=100, verbose_name="Telefon")
address = models.CharField(max_length=250, verbose_name="Adresi")
message = models.CharField(max_length=1000, verbose_name="Mesaj")
email = models.EmailField(max_length=40, verbose_name="E-Posta")
province= models.CharField(max_length=40,verbose_name="Şehir")
tarih = models.DateTimeField(default=None)
basvuru = models.CharField(max_length=2, choices=DURUM)
sinif = models.CharField(max_length=2, choices=SINIF)
class Meta:
ordering = ["tarih", "email"]
verbose_name = "Mesaj"
verbose_name_plural = "Mesajlar"
def __str__(self):
return self.basvuru
My forms folder
from django.shortcuts import redirect, render
from django.forms import ModelForm
from apps.giris.models import Iletisim
class Kesif_Form(ModelForm):
class Meta:
model = Iletisim
fields = '__all__'
def kesif_kayit(request):
if request.method=='POST':
form = Kesif_Form(request.POST)
if form.is_valid():
yeni_kesif = Iletisim()
yeni_kesif.name = request.POST.get("name")
yeni_kesif.phone = request.POST.get("phone")
yeni_kesif.address = request.POST.get("address")
yeni_kesif.message = request.POST.get("message")
yeni_kesif.email = request.POST.get("email")
yeni_kesif.province = request.POST.get("province")
yeni_kesif.tarih = request.POST.get("tarih")
yeni_kesif.basvuru = request.POST.get("basvuru")
yeni_kesif.sinif = request.POST.get("sinif")
yeni_kesif.save()
return redirect('index')
else:
form = Kesif_Form()
context={'form' : form}
return render(request,'index.html',context)
Main class
...
from forms.formlar import Kesif_Form
class Anasayfa_View(TemplateView, Kesif_Form):
template_name = 'index.html'
def get_context_data(self, **kwargs):
...
context['kform'] = Kesif_Form
...
return context
index.html
...
{{ kform.errors }}
<form action="{% url 'kesif' %}" method="POST" class="php-email-form php-email-form animate__animated animate__fadeInRight">
{{ kform.errors }}
{{ kform }}
<input type="submit" value="gir">
</form>
...
urls.py
...
from forms.formlar import kesif_kayit
urlpatterns = [
path('admin/', admin.site.urls),
path('', Anasayfa_View.as_view(), name='grsndx'),
path('kesif/', kesif_kayit, name='kesif'),
path('<str:ktgr>/', Detaylar_View.as_view(), name='dtyndx'),
path('<str:ktgr>/<str:bslk>/', Detay_View.as_view(), name='dtydty'),
]
and submit not work, every fields in form, I want 2 forms but I have not work anyone.
help..
structure
-project
-apps
-giris
-models.py
+detay
...
+env
-fls
-settings
-urls
-forms
-formlar.py
...
-templates
-index.html
...
-manage.py
...
If you are using Django model form than you have to do something like this first create form class
class Kesif_Form(ModelForm):
class Meta:
model = Iletisim
fields = '__all__'#here you to defined specific you field or all the field
than you have to submit it like this
def kesif_kayit(request):
if request.method=='POST':
form = Kesif_Form(request.POST)
if form.is_valid():
form.save()
return redirect('index')
else:
form = Kesif_Form()
context={'form' : form}
return render(request,'index.html',context)
you don't have save it manually Django Model Form will save it for you.

How can i call a list in django template?

Here is my models.py file.
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.text import slugify
import misaka
from django.contrib.auth import get_user_model
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(allow_unicode=True, unique=True)
description = models.TextField(blank=True, default='')
description_html = models.TextField(editable=False, default='',blank=True)
members = models.ManyToManyField(User, through="CategoryMember")
category_pic = models.ImageField(upload_to = 'category_pics', blank=True)
def __str__(self):
return self.name
# WE are saving the model. But before that we are converting
# the name using slugify and description using misaka.
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
self.description_html = misaka.html(self.description)
super().save(*args, **kwargs)
#get_absolute_url is used because it tell the template
# CreateView to go to the page it is directing.
# for this example it is directing to go to single page of a category.
def get_absolute_url(self):
return reverse("categories:single", kwargs={"slug":self.slug})
class Meta:
ordering = ['name']
class CategoryMember(models.Model):
category = models.ForeignKey(Category, related_name = "memberships", on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name="user_categories", on_delete=models.CASCADE)
def __str__(self):
return self.user.username
class Meta:
unique_together= ("category", "user")
This is some part of views.py file
from django.contrib.auth.models import User
from categories.models import CategoryMember, Category
class UserPosts(ListView):
model = Post
template_name = 'posts/user_post_list.html'
def get_queryset(self):
try:
self.post_user = User.objects.prefetch_related("user_of_post_model").get(
username__iexact=self.kwargs.get("username")
)
except User.DoesNotExist:
raise Http404
else:
return self.post_user.user_of_post_model.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
current_user = UserProfileInfo.objects.filter(
user__id__iexact = self.post_user.id
).get()
i=1
user_categories={}
for member in CategoryMember.objects.all():
if member.user == self.post_user:
user_categories.update({i:member.category.name})
i=i+1
else:
print('not found')
print(user_categories)
if current_user.profile_pic:
profile_pic = True
picture = current_user.profile_pic
edited_picture = get_thumbnail(picture, '350x350', quality=99, format='PNG')
else:
profile_pic = False
root = settings.MEDIA_ROOT
import os
root1 = os.path.join(root, 'profile_pics/no-image.png')
picture = root1
edited_picture = get_thumbnail(picture, '350x350', quality=99, format='PNG')
# resizeimage.resize_cover(picture, [200, 100], validate=False)
# rescale_image(picture,width=100,height=100)
current_user1 = current_user
user_info = {
'current_user':current_user,
'user_categories':user_categories,
'picture':edited_picture,
'profile_pic':profile_pic,
'post_user':self.post_user,
}
context['user_info'] = user_info
return context
After rendering the html page i can see that dictionary has values inside. Here is the values printed in terminal.
not found
not found
not found
not found
not found
{1: 'Regression', 2: 'Classification'}
Now, i want to get user_categories in my django template. i have tried in different approaches but can't get user_categories .
this is some part of my django html file.
<h3>Member of Categories</h3>
{# this line shows error #}
{# {{ userinfo[{{user_categories}}] }} #}
{{ userinfo.user_categories.1 }}
{% for categories in userinfo.user_categories %}
{{categories}}
{% endfor %}
<h5 class="techfont">Post written by {{user_info.post_user.username}} : {{post_list.count}} </h5>
This section only renders user_info.post_user.username but not user_info.user_categories.1
see the text below.
Member of categories.
Post written by abc: 1
user_categories is a dictionary, not a list. So try like this:
{% for idx, categories in userinfo.user_categories.items %}
{{idx}}. {{categories}}
{% endfor %}

Django many to many relationsip update field

I have two models many to many relationships, I am trying to update a field by subtraction two values from the two models and save the changes to the db.
class LeaveBalance(models.Model):
user=models.OneToOneField(User,on_delete=models.CASCADE,primary_key=True,)
Leave_current_balance= models.FloatField(null=True, blank=True, default=None)
Year=models.CharField(max_length=100,default='')
def __unicode__(self):
return self.Year
class NewLeave(models.Model):
user=models.ForeignKey(User,default='',on_delete=models.CASCADE)
leave_balance=models.ManyToManyField(Leave_Balance)
leave=(
('annual','annual'),
('sick','sick'),
)
Leave_type=models.CharField(max_length=100,choices=leave,blank=False,default='')
Total_working_days=models.FloatField(null=True, blank=False)
DirAuth=(
('Pending','Pending'),
('Approved','Approved'),
('Rejected','Rejected'),
)
Director_Authorization_Status=models.CharField(max_length=100,choices=DirAuth,default='Pending',blank=False)
Date_Authorized=models.DateField(null=True,blank=False)
Authorized_by_Director=models.CharField(max_length=100,default='',blank=False)
def __unicode__(self):
return self.Leave_type
here is my form, when a leave is submitted the director is notified by email. the director can login to the system to approve the leave using the form. once the leave is approved, I want to adjust the Leave_current_balance.
class DirectorForm(forms.ModelForm):
class Meta:
model=NewLeave
fields=('Director_Authorization_Status','Authorized_by_Director','Date_Authorized',)
widgets={
'Date_Authorized':DateInput()
}
This is the function that allows the director to approve the leave which throws the error: u'Leave_current_balance'
def unitDirectorForm(request,id):
if request.method=='POST':
getstaffid=NewLeave.objects.get(id=id)
form = DirectorForm(request.POST, instance=getstaffid)
if form.is_valid():
getstaffid = form.save(commit=False)
getstaffid.save()
total_days = getstaffid.Total_working_days
current_balance = getstaffid.user.leave_balance.Leave_current_balance
diff_balance = current_balance - total_days
current_balance = diff_balance
current_balance=form.fields['Leave_current_balance']
current_balance.save()
getstaffid.leave_balance.add(current_balance)
return HttpResponse('You have successfuly Authorise the leave')
else:
#getstaffid=NewLeave.objects.get(id=id)
form=DirectorForm()
#c_balance=Leave_Balance.objects.get()
balance_form = leavebbalanceForm()
return render(request,'managerauthorisedform.html',{'form':form})
You could get this working in another way too. For example:
def on_balance(user_id):
id = user_id
c_balance = LeaveBalance.objects.get(user=id)
current_balance = c_balance.Leave_current_balance
t_days = NewLeave.objects.get(user=id)
total_days = t_days.Total_working_days
current_balance = current_balance - total_days
balance = LeaveBalance.objects.get(user=id)
balance.leave_balance = current_balance
balance.save()
And the above does not cause combined expression error.
Or just a bit simpler:
def on_balance(user_id):
id = user_id
c_balance = LeaveBalance.objects.get(user=id)
current_balance = c_balance.Leave_current_balance
t_days = NewLeave.objects.get(user=id)
total_days = t_days.Total_working_days
current_balance = current_balance - total_days
c_balance.leave_balance = current_balance
c_balance.save()
UPDATE - restructuring the models and views
So the above code works if it's used in an appropriate model/form/view structure, but instead I would suggest you to restructure the whole thing starting from your models. I give you a simple working example (I tested this and works):
My app name is in this example: Myusers1 , so where ever you see that, you can change that name to your app name if needed.
So the Models:
from django.db import models
from django.conf import settings
from django.utils.text import slugify
from django.db.models import F
from django.urls import reverse
class Director(models.Model):
name = models.CharField(max_length = 100, default = '', null = True, verbose_name = 'Name of Director')
def __str__(self):
return self.name
class Staff(models.Model):
TYPE_CHOICES = (
('REGULAR', 'Regular'),
('MANAGER', 'Manager'),
('FRESH', 'Fresh'),
)
name = models.CharField(max_length = 100, default = '', null = True, unique=True, verbose_name = 'Name of staff member')
birthdate = models.DateField(blank = True, verbose_name = 'Birth date')
department = models.CharField(max_length = 100, default = '', null = True, verbose_name = 'Department')
# department could also be a choice field from another table
type = models.CharField(max_length = 20, choices = TYPE_CHOICES, verbose_name = 'Position Type', null = True)
def __str__(self):
return self.name
class LeaveBalance(models.Model):
staff = models.ForeignKey(Staff, to_field='name', on_delete = models.CASCADE, primary_key = False)
Leave_current_balance = models.FloatField(null = True, blank = True, default = '')
date_updated = models.DateTimeField(auto_now_add = True, verbose_name = 'Last Updated date and time')
def __unicode__(self):
return self.Leave_current_balance
class NewLeave(models.Model):
all_staff = Staff.objects.values()
STAFF_CHOICES = [(d['name'], d['name']) for d in all_staff]
staff = models.CharField(max_length = 100, choices = STAFF_CHOICES)
leave_days_to_approve_now = models.FloatField(null = True, blank = False, default = 5.0, verbose_name = 'Leave days for approval now')
LEAVE_CHOICES=(
('annual','annual'),
('sick','sick'),
)
Leave_type = models.CharField(max_length = 100, choices = LEAVE_CHOICES, blank = False, default = '', verbose_name = 'Type of leave')
Total_working_days = models.FloatField(null = True, blank = False, default = 200.0)
APPROVAL_STATUS_CHOICES=(
('Pending','Pending'),
('Approved','Approved'),
('Rejected','Rejected'),
)
Director_Authorization_Status = models.CharField(max_length = 100, choices = APPROVAL_STATUS_CHOICES, default = 'Pending', blank = False)
Date_Authorized = models.DateTimeField(auto_now_add = True, verbose_name = 'date and time of Authorization')
all_directors = Director.objects.values()
DIRECTOR_CHOICES = [(d['name'], d['name']) for d in all_directors]
Authorized_by_Director = models.CharField(max_length = 100, choices = DIRECTOR_CHOICES, default = '', blank = False)
def __unicode__(self):
return self.Leave_type
def get_absolute_url(self):
pass
# return reverse('newleave-detail', kwargs={'pk': self.pk}) # this should be worked out too
def save(self, *args, **kwargs):
staff_name = self.staff
this_staff = Staff.objects.get(name=staff_name)
name = this_staff.name
minus_value = self.leave_days_to_approve_now
if (self.Director_Authorization_Status == 'Approved'):
LeaveBalance.objects.filter(staff = name).update(Leave_current_balance=F('Leave_current_balance') - minus_value)
return super(NewLeave, self).save(*args, **kwargs)
else:
return super(NewLeave, self).save(*args, **kwargs)
In the above, you can see that I created a Director and Staff Model in which you can set as many staff and directors as you want in the Admin back-end. I created the staff Model because maybe not all of the staff will be users, so I think it is just a bit better to keep them separately in the DB from Users.
Important: first create the Director and Staff Models then migrate immediately since the other two tables will depend on them. Then you can create the other two Models.
I also do not think that in the LeaveBalance Model you should keep more things than what I put there. I think Year field for example redundant, since you can always filter by the date and date range in you want in the database.
Then the views (I used simple views only directly from the Models). With using these view classes you do not have to create Forms since it is automatically created from the Models and you can handle them with different functions/methods as Forms in the Views and in the Model classes.
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect, HttpResponse, HttpRequest
from django.urls import reverse
from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic import ListView, TemplateView
from django.template import loader
from .models import NewLeave
from django.views.generic.edit import FormView, CreateView, DeleteView, UpdateView
from django.urls import reverse_lazy
class NewLeaveCreate(CreateView):
model = NewLeave
fields = '__all__'
def form_valid(self, form):
super().form_valid(form)
auth_status = form.cleaned_data['Director_Authorization_Status']
if (auth_status == 'Approved'):
return redirect('Myusers1:success_page')
elif (auth_status == 'Pending'):
return redirect('Myusers1:pending_success')
else:
return redirect('Myusers1:rejected_success')
class NewLeaveUpdate(UpdateView):
model = NewLeave
fields = '__all__'
class NewLeaveDelete(DeleteView):
model = NewLeave
success_url = reverse_lazy('newleave-list')
class NewLeaveDetail(DetailView):
model = NewLeave
template_name = 'myusers1/newleave_detail.html'
context_object_name = 'newleave'
queryset = NewLeave.objects.all()
def get_context_data(self, **kwargs):
context = super(NewLeaveDetail, self).get_context_data(**kwargs)
context['leave_details'] = NewLeave.objects.filter(pk=pk)
return context
class Success(TemplateView):
template_name = "authorizationsuccess.html"
class pending_success(TemplateView):
template_name = "pendingsuccess.html"
class rejected_success(TemplateView):
template_name = "rejectedsuccess.html"
Then in urls.py I defined the required urls:
from django.urls import path, re_path
from . import views
from . import models
app_name = 'Myusers1'
urlpatterns = [
path('newleave/add/', views.NewLeaveCreate.as_view(), name='newleave-add'),
path('newleave/<int:pk>/', views.NewLeaveUpdate.as_view(), name='newleave-update'),
path('newleave/<int:pk>/delete/', views.NewLeaveDelete.as_view(), name='newleave-delete'),
# path('newleave/add/<int:pk>/', views.NewLeaveDetail.as_view(), name='newleave-detail'),
path('newleave/add/success/', views.Success.as_view(), name='success_page'),
path('newleave/add/pendingleaves/', views.pending_success.as_view(), name='pending_success'),
path('newleave/add/rejectedleaves/', views.rejected_success.as_view(), name='rejected_success'),
]
I have not worked out all of the url paths.
And the templates like the newleave_form.html
{% extends 'myusers1/base.html' %}
{% block content %}
<div class"container">
<div class="col col-lg-2">
<h2>New Leave Form</h2>
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Authorize</button>
</form>
</div>
</div>
{% endblock %}
And there should be at least 3 different redirect template when a they submit a NewLeave form, authorized, pending, and rejected templates. I just give here the simple authorized success template:
{% extends 'myusers1/base.html' %}
{% block content %}
<h2>Thank you! The authorization of leave was successful</h2>
<div class="col-xs-12 .col-md-8"><li> Back to Home </li></div>
{% endblock %}
Do not forget to migrate and then register the models in the admin.py. Then you should create some staff in the database and few directors to try the above. I hope that the above can give you some direction to accomplish what you are up to with your project. With this I just wanted to give you a very simple example. (You have to create all of the other necessary templates and views).
If you create a new app for trying the above then in your project main urls.py file you should reference (include) your app urls like this with adding one extra line to your project' urls.py file. Then all of your new app urls has to be defined in your app's urls.py file:
This is how your main project's urls.py looks like then:
urlpatterns = [
path('admin/', admin.site.urls),
path('myusers1/', include('Myusers1.urls')),
# so in your case:
path('myapp/', include('myapp.urls')),
]
(you have to change the Myusers1 to your app name,)
And of course we could do a lot of other things with the Model Manager in Django: https://docs.djangoproject.com/en/2.1/topics/db/managers/
You have to call refresh_from_db on balance. like balance.refresh_from_db() to get the updated values from database.

type object 'Album' has no attribute 'object'

I'm new to Django and web coding.
I'm following Bucky tuts: Django Tutorial for Beginners - 29 - Generic Views
& I'm trying to get my music ( index ) page , but it gives me that error in the browser :
AttributeError at /music/ type object 'Album' has no attribute
'object'
& here's my views.py :
from django.http import HttpResponse, Http404
from django.shortcuts import render , get_object_or_404
from .models import Album,song
from django.views import generic
"""
def index(request):
all_albums = Album.objects.all()
context = {'all_albums': all_albums}
return render(request, 'music/index.html', context)
"""
class IndexView (generic.ListView):
template_name = 'music/index.html'
context_object_name = 'all_albums'
def get_queryset(self):
return Album.object.all()
'''
class DetailView (generic.DetailView):
model = Album
template_name = "music/details.html"
'''
def details(request, album_id):
try:
album = Album.objects.get(pk=album_id)
except Album.DoesNotExist:
raise Http404("Album Does Not Exists !")
return render(request, 'music/details.html', {'album': album})
def favourite (request , album_id):
album = get_object_or_404 (Album , pk=album_id)
try:
selected_song = album.song_set.get(pk=request.POST['song'])
except(KeyError, song.DoesNotExist):
return render(request, 'music/details.html', {
'album':album,
'error_message': "you entered wrong"
})
else:
selected_song.is_favorite = False
selected_song.save()
return render(request,'music/details.html' , {'album':album})
models.py
from django.db import models
# Create your models here.
class Album (models.Model):
artist = models.CharField(max_length = 100)
album_title = models.CharField(max_length = 100)
genre = models.CharField(max_length = 50)
album_logo = models.CharField(max_length = 1000)
def __str__(self):
return self.album_title + " - " + self.artist
class song (models.Model):
album = models.ForeignKey(Album , on_delete=models.CASCADE)
file_type = models.CharField(max_length = 10)
song_title = models.CharField(max_length = 100)
is_favourite = models.BooleanField(default=False)
def __str__(self):
return self.song_title
index.html
{% extends 'music/base.html' %}
{% block title %}Main : MuSiC {% endblock %}
{% block body %}
<ul>
{% for album in all_albums %}
<li>{{ album.album_title }}</li>
{% endfor %}
</ul>
{% endblock %}
#/music/{{ album.id }}
project structure
{ (website) project dir }
|-music
..|-migrations
..|-static
..|-templates
....|-music
......|-base.html
......|-details.html
......|-index.html
|-__init__.py
|-admin.py
|-apps.py
|-models.py
|-tests.py
|-urls.py
|-views.py
|-website
..|-__init__.py
..|-settings.py
..|-urls.py
..|-wsgi.py
|-db.sqlite3
|-manage.py
and I don't know where is the problem :(
btw, lot's of coding terms I still didn't learned , that's why I may ask alot ever I searched for a solution but didn't understand the answer from other question's answers .
Album.object does not exist; you should've written Album.objects.
class IndexView (generic.ListView):
template_name = 'music/index.html'
context_object_name = 'all_albums'
def get_queryset(self):
# return Album.object.all() <-- Remove this
return Album.objects.all()
As a side note, reserved words cannot be python attributes. This is by design, because disallowing these words makes parsing substantially easier.
Why can't attribute names be Python keywords?

Categories