Models
class Head_of_department(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.CharField(max_length=30)
def __str__(self):
return self.first_name
class Employee(models.Model):
first_name = models.CharField(max_length=200, unique=True)
last_name = models.CharField(max_length=200, unique=True)
head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
email = models.EmailField(max_length=100)
def __str__(self):
return self.first_name + ' ' + self.last_name
class Attendance(models.Model):
head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
employee = models.ForeignKey('Employee', on_delete=models.CASCADE, )
attendance = models.CharField(max_length=8, choices=attendance_choices, blank=True)
Views
class Attendancecreate(CreateView):
model = Attendance
fields = ['employee']
success_url = '/dashboard/'
def get_context_data(self,** kwargs):
context = super(Attendancecreate, self).get_context_data(**kwargs)
email = self.request.user.email
hod = Head_of_department.objects.get(email=email)
context["objects"] = self.model.objects.filter(employee__head_of_department =hod)
print (context["objects"])
return context
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.head_of_department = get_object_or_404(Head_of_department, email=self.request.user.email)
self.object.save()
return super().form_valid(form)
Template
<div class="form-group">
{% for item in objects %}
{{ item.employee }}
{% endfor %}
</div>
The webapp has a login feature. The headofdepartment can mark the attendance . I want to render a list of employees under the respective logged in HOD and mark attendance . I want to do this in the same view .
I am unable to render the employees for logged in HOD.
I found the solution , I wrote views incorrectly.
model = Attendance
fields = ['employee']
success_url = '/dashboard/'
def get_context_data(self,** kwargs):
context = super(Attendancecreate, self).get_context_data(**kwargs)
email = self.request.user.email
hod = Head_of_department.objects.get(email=email)
context["objects"] = Employee.objects.filter(head_of_department =hod)
print (context["objects"])
return context
Template
{% for emp in objects %}
{{ emp.first_name }} {{ emp.last_name }}
{% endfor %}
Related
I am trying to create a JobRequestFile form where you can upload multiple files involving a foreign key to my JobRequest model. However, when trying to save, it takes me to my JobRequest form and says these fields are required.
my form:
class Meta:
model = JobRequestFile
fields = [
'file'
]
widgets = {
'file': ClearableFileInput(attrs={'multiple': True}),
}
my view:
class FileUpload(FormView):
form_class = JobRequestFileForm
template_name = 'jobrequest/upload.html' # Replace with your template.
success_url = 'loggedIn'
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('file')
if form.is_valid():
most_recent_jr = JobRequest.get.latest('requestDate')
for f in files:
JobRequestFile.object.create(jobRequest=most_recent_jr, file=f)
return self.form_valid(form)
else:
return self.form_invalid(form)
My template:
<form action='.' method='POST' enctype='multipart/form-data'>
{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save' />
</form>
The two models:
class JobRequest(models.Model):
customer = models.ForeignKey('account.Customer', on_delete=models.CASCADE)
partnerTypeNeeded = models.CharField('PartnerTypeNeeded', max_length=30, choices=PartnerMore.PartnerTypes.choices)
issueDescription = models.TextField(blank=True)
communa = models.CharField(unique=False, max_length=30, default='0')
serviceDate = models.DateTimeField()
requestDate = models.DateTimeField(auto_now_add=True, null=False, blank=False)
budget = models.FileField(upload_to='budget/%Y/%m/', null=True, blank=True)
jobCompleted = models.BooleanField(default=False)
partner = models.ForeignKey('account.Partner', on_delete=models.SET_NULL, null=True, blank=True, related_name='assignedPartner')
suggestedPartner = models.ForeignKey('account.Partner', on_delete=models.SET_NULL, null=True, blank=True, related_name='suggestion')
class JobRequestFile(models.Model):
jobRequest = models.ForeignKey('JobRequest', on_delete=models.CASCADE)
file = models.FileField(upload_to='files/%Y/%m/')
The general task: there is a list of the events which are displayed in a table view, the user clicks on the event and gets redirected to a single event page, which displays extra information about an event.
There is a button on this template that should give functionality to record some data of the event in the user-selected events list.
And I really don't get how to pass the event data into the form to create the user_event, maybe I chose the wrong way to create the instance of the model?
Models.py
class Event(models.Model):
LANGUAGE_CHOICES = [
('EN', 'English'), ('IT', 'Italian'), ('FR', 'French'), ('DE', 'German'), ('RU', 'Russian'), ]
STATUS_CHOICES = [('draft', 'Draft'), ('confirmed', 'Confirmed'), ]
FORMAT_CHOICES = [('live', 'Live'), ('online', 'Online'), ]
TOPIC_CHOICES = [('cataract', 'Cataract'),
('vitreo', 'Vitreoretinal'),
('optometry', 'Optometry'),
('multi', 'Multidisciplinary'),
('plastic', 'Plastic and Reconstructive surgery'),
('onco', 'Ocular oncology'),
('glaucoma', 'Glaucoma'),
('pediatrics', 'Pediatrics'), ]
title = models.CharField(
max_length=250, verbose_name='Event', db_index=True, default="Event")
slug = models.SlugField(max_length=250, unique_for_date='publish')
kol = models.ManyToManyField(
Kol, verbose_name='Presenter', db_index=True, blank=True)
body = models.TextField(verbose_name='Description', blank=True)
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
sponsor = models.CharField(
max_length=150, verbose_name='Sponsored by', default='')
organizer = models.CharField(
max_length=150, verbose_name='Organized by', default='')
topic = models.CharField(
max_length=100, choices=TOPIC_CHOICES, default='multi', verbose_name='Discipline')
status = models.CharField(
max_length=100, choices=STATUS_CHOICES, default='draft')
event_format = models.CharField(
max_length=50, choices=FORMAT_CHOICES, default='online', verbose_name='Format', db_index=True)
main_language = models.CharField(
max_length=50, choices=LANGUAGE_CHOICES, default='EN', verbose_name='Main language', db_index=True)
event_url = models.URLField(blank=True)
event_date_start = models.DateField(
verbose_name='Start date', db_index=True)
event_time_start = models.TimeField(default='09:00')
event_date_finish = models.DateField(
verbose_name='End date', auto_now=True)
event_time_finish = models.TimeField(default='18:00')
is_cme = models.BooleanField(
default=False, verbose_name='CME credits', db_index=True)
objects = models.Manager() # the default manager
published = PublishedManager() # published manager
futured = FutureManager() # future dates only
# follow check
users_follow = models.ManyToManyField(
settings.AUTH_USER_MODEL, related_name='events_liked', blank=True)
class Meta:
ordering = ('event_date_start',)
def __str__(self):
return self.title
def get_kols(self):
return "\n".join([str(p) for p in self.kol.all()])
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Event, self).save(*args, **kwargs)
#property
def _month(self):
return datetime.date(self.event_date_start).month
event_month = property(_month)
def get_absolute_url(self):
return reverse('events:event_detail', args=[self.publish.year,
self.publish.strftime(
'%m'),
self.publish.strftime(
'%d'),
self.slug])
class UserEvent(models.Model):
follower = models.ForeignKey(
User, related_name='user_events', on_delete=models.CASCADE)
my_event = models.ForeignKey(
Event, related_name='selected_event', on_delete=models.CASCADE)
title = models.CharField(
max_length=250, verbose_name='Event', db_index=True, default="Event")
event_url = models.URLField(blank=True)
event_date_start = models.DateField(
verbose_name='Start date')
event_date_finish = models.DateField(
verbose_name='End date')
class Meta:
ordering = ['-event_date_start']
def save(self, *args, **kwargs):
created = not self.pk
super().save(*args, **kwargs)
if created:
UserEvent.objects.create(user=self)
def __str__(self):
return self.title
forms.py
class UserEventForm(forms.ModelForm):
class Meta:
model = UserEvent
fields = ("title", "event_url",
"event_date_start", "event_date_finish")
detail.html with the button
{% extends 'bbase11.html' %} {% block title %} {{ event.title}} {% endblock %} {% block content %}
<div class="container">
<h1> {{ event.title }}</h1>
<p class='event_descr'> {{event.body}}</p>
<p> Presenting professionals {{event.get_kols}}</p>
<div>Organized by {{event.organizer}}</div>
<div>Sponsored by {{event.sponsor}}</div>
<div>
<form action= "{% url "events:add_event" %}" method = "POST" >
{{form.as_p}}
{% csrf_token %}
<input id='event' type='hidden' value = {{event.id}}>
<button type="submit" class="btn btn-outline-primary btn-sm m-0 waves-effect">Add to calendar</button></div>
</form>
</div>
{% endblock %}
views.py
def event_detail(request, year, month, day, event):
event = get_object_or_404(Event, slug=event,
status='confirmed',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request, 'events/event/detail.html', {'event': event, 'add_event': add_event})
#login_required
def add_event(request):
if request.method == 'POST':
form = UserEventForm(request.POST or None)
if form.is_valid():
form.save()
messages.success(request, ("Event has been added"))
return redirect('event_list')
else:
messages.error(request, ("Event has not been added"))
return redirect('events:event_list')
else:
events = Event.futured.all()
return render(request, 'list.html', {'events': events})
Using a class based view instead of your function based one, you can override the get_initial method as follows:
class AddEventCreateView(CreateView):
model = Event
form_class = UserEventForm
template_name = 'your/template.html'
def get_initial(self):
initial = super().get_initial()
# Add your initial values here - for example
initial['title'] = 'Some value'
return initial
def form_valid(self, form):
self.object = form.save(commit=False)
# Do something here
return super().form_valid(form)
def get_success_url(self):
return reverse_lazy('your:url-name')
You can now pass as many initial values to your form as needed. See http://ccbv.co.uk/projects/Django/3.0/django.views.generic.edit/CreateView/#get_initial for details.
It looks like it may be a typo in the URL in the detail.html with the quotes.
Change this:
<form action= "{% url "events:add_event" %}" method = "POST" >
To this:
<form action="{% url 'events:add_event' %}" method="POST">
I'm new to django; I'm unable to display current login user details on profile template from model. When I will try with Maas.objects.all(), I get all existing user's data also. I need to display only the current login user data on template. When I try with Maas.objects.get(username=username), I get an error
Type error: Maas.testapp.models doesn't exist match query
My views.py:
def maas(request,maas_username_slug):
context_dict = {}
try:
maas = Maas.objects.get(slug=maas_username_slug)
context_dict['maas_username'] = maas.username
context_dict['maas_username_slug'] = maas_username_slug
context_dict['maas_phone'] = maas.phone
context_dict['maas_firstname'] = maas.firstname
context_dict['maas_lastname'] = maas.lastname
context_dict['maas_location'] = maas.location
context_dict['date_of_birth'] = maas.date_of_birth
context_dict['comments'] = maas.comments
context_dict['maas_gender'] = maas.gender
context_dict['photo'] = maas.photo
context_dict['maas'] = maas
except Maas.DoesNotExist:
pass
print(context_dict)
return render(request, 'testapp/profile.html', context_dict)
My profile.html:
<!DOCTYPE html>
{%extends 'testapp/base.html'%}
{%block body_block%}
<h1>Profile page</h1>
<h1>{{ maas_username }}</h1>
<li>Phone: {{ maas_phone }} </li>
<li>firstname: {{ maas_firstname }} </li>
<li>lastname: {{ maas_lastname }} </li>
<li> gender: {{ maas_gender }} </li>
<li> Comments: {{ comments }} </li>
{%endblock%}
My models.py:
from django.db import models
from django.template.defaultfilters import slugify
from django.contrib.auth.models import User
class Maas(models.Model):
username=models.CharField(max_length=25)
password = models.CharField(max_length=50)
phone = models.IntegerField(default=0)
firstname = models.CharField(max_length=40, blank=True)
lastname = models.CharField(max_length=40, blank=True)
location = models.CharField(max_length=40, blank=True)
email = models.EmailField(blank=True)
date_of_birth = models.DateField(null=True, blank=True)
gender = models.CharField(max_length=10, blank=True)
comments = models.CharField(max_length=500, blank=True)
photo = models.ImageField(upload_to='media/photos/', null=True,
blank=True)
slug = models.SlugField(unique=True,default="")
def __str__(self):
return self.username
def save(self, *args, **kwargs):
self.slug = slugify(self.username)
super(Maas, self).save(*args, **kwargs)
def __unicode__(self):
return self.username
class UserProfile(models.Model):
user = models.OneToOneField(User)
nickname = models.CharField(max_length=20, blank=True)
def __unicode__(self):
return self.user.username
My urls.py
urlpatterns = [
url(r'(?P<maas_username_slug>\w+)/$', views.maas,
name='profile'),
]
Not getting any error but not displaying data on profile.html
Models
attendance_choices = (
('absent', 'Absent'),
('present', 'Present')
)
class Head_of_department(models.Model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
email = models.CharField(max_length=30)
def __str__(self):
return self.first_name
class Employee(models.Model):
first_name = models.CharField(max_length=200, unique=True)
last_name = models.CharField(max_length=200, unique=True)
head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
email = models.EmailField(max_length=100)
def __str__(self):
return self.first_name + ' ' + self.last_name
class Attendance(models.Model):
head_of_department = models.ForeignKey('Head_of_department', on_delete=models.SET_NULL, blank=True, null=True)
employee = models.ForeignKey('Employee', on_delete=models.CASCADE, )
attendance = models.CharField(max_length=8, choices=attendance_choices, blank=True)
Views
class Attendancecreate(CreateView):
model = Attendance
fields = ['employee']
success_url = '/dashboard/'
def get_context_data(self,** kwargs):
context = super(Attendancecreate, self).get_context_data(**kwargs)
context['formset'] = AttendanceFormset(queryset=Attendance.objects.none(), instance=Head_of_department.objects.get(email=email), initial=[{'employee': employee} for employee inself.get_initial()['employee']])
context['attendance_form'] = Attendanceform()
email = self.request.user.email
hod = Head_of_department.objects.get(email=email)
context["employees"] = Employee.objects.filter(head_of_department =hod)
return context
def get_initial(self):
email = self.request.user.email
hod = Head_of_department.objects.get(email=email)
initial = super(Attendancecreate , self).get_initial()
initial['employee'] = Employee.objects.filter(head_of_department=hod)
return initial
def post(self, request, *args, **kwargs):
formset = AttendanceFormset(queryset=Attendance.objects.none(), instance=Head_of_department.objects.get(email=email), initial=[{'employee': employee} for employee inself.get_initial()['employee']))
if formset.is_valid():
return self.form_valid(formset)
def form_valid(self, formset):
instances = formset.save(commit=False)
for instance in instances:
instance.head_of_department = get_object_or_404(Head_of_department, email=self.request.user.email)
instance.save()
return HttpResponseRedirect('/dashboard/')
def form_invalid(self, formset):
print ('errors')
print (formset.errors)
Forms
class Attendanceform(ModelForm):
class Meta:
model = Attendance
fields = ('employee','attendance','hod')
AttendanceFormset = inlineformset_factory(Head_of_department,Attendance,fields=('attendance',))
Template
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form }}
<br><br>
{% endfor %}
Error
Below shown square brackets was printed in the console when I used print(formset.errors)
[]
How to pass employees as initial values for attendance model as shown below in the images, employees will be rendered and rendered values have to be passed as employee in attendance model.
Is there any workaround with modelformset or inlineformset ? I can't get it right with the views I have included in the question .
I was missing request.post ,
class Attendancecreate(CreateView):
...
def post(self, request, *args, **kwargs,):
formset = AttendanceFormset(request.POST,queryset=Attendance.objects.none(), instance=Head_of_department.objects.get(email=self.request.user.email), initial=[{'employee': employee} for employee in self.get_initial()['employee']])
if formset.is_valid():
return self.form_valid(formset)
Template
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form.employee.initial }} {{ form.employee}} {{ form.attendance }}
<br><br>
{% endfor %}
I'm new in django.I want to retrieve all the records from related models and display the dynamic data in templates. I tried by using raw sql also but i could not display dynamic data templates.
models.py:
class NewRegistration(models.Model):
houseowner_name_en = models.CharField(max_length=30)
ward_no = models.ForeignKey(system_settings.models.Wardno)
contactno = models.CharField(max_length=30)
construction_type = models.ForeignKey(system_settings.models.ConstructionType)
taxpayer_id = models.CharField(max_length=30, blank=True, null=True)
cen = models.IntegerField()
is_forwarded = models.BooleanField(default=False)
class Application(models.Model):
registration_date = models.CharField(max_length=15)
building_use = models.ForeignKey(to=system_settings.models.BuildingUse)
building_category = models.CharField(max_length=30)
building_structure_category = models.ForeignKey(to=system_settings.models.BuildingStructureCategory)
building_storey = models.IntegerField(blank=True, null=True, default=0)
reg = models.ForeignKey(NewRegistration)
class Landowner(models.Model):
landowner_type = models.CharField(max_length=30)
lo_salutation = models.CharField(max_length=30)
lo_name_np = models.CharField(max_length=30)
lo_citizenship_issue_date = models.CharField(max_length=30)
reg = models.ForeignKey(NewRegistration)
views.py:
def export(request):
all_objects = NewRegistrationModel.objects.all()
# houseowner= all_objects.houseownerinfo_set.all()
app_all=Application.objects.all()
landinfo=Landinfo.objects.all()
return render(request, 'exports.html', {'all_objects': all_objects})
I did it like this.
views.py
def export(request):
all_objects = NewRegistration.objects.all()
return render(request, 'exports.html', {'all_objects': all_objects})
exports.html
# in template when displaying
{% for registration in all_objects %}
{{ registration.fiscalyear }}
{% for owner in registration.landowner_set.all %}
{{owner.landowner_type}}
{{owner.lo_wardno}}
{% endfor %}
{% endfor %}
you can use generic.ListView in your views.py file like that:
class Export(generic.ListView):
model = NewRegistration
template_name = "your_template_name.html"
def get_queryset(self):
return NewRegistration.objects.all()
def get_context_data(self, **kwargs):
context = super(Export, self).get_context_data(**kwargs)
context['app_all'] = Application.objects.all()
context['landinfo'] = Landingo.objects.all()
return context
you can accees the datas in your template like that {{ app_all }} or {{ landinfo }}.