Commit 30ae1dca authored by Jonathan Talbot's avatar Jonathan Talbot

lab3

parent 1a099ef6
No preview for this file type
from django import forms
...@@ -10,7 +10,7 @@ class Department(models.Model): ...@@ -10,7 +10,7 @@ class Department(models.Model):
return '{}, {}'.format(self.dept_name, self.home_unit) return '{}, {}'.format(self.dept_name, self.home_unit)
class WidgetUser(models.Model): class WidgetUser(models.Model):
id_num = models.CharField(max_length=7, validators=[RegexValidator(r'^\d{1,10}$')]) id_num = models.CharField(max_length=7, validators=[RegexValidator(r'^\d{1,10}$')], primary_key=True)
first_name = models.CharField(max_length=90) first_name = models.CharField(max_length=90)
middle_name = models.CharField(max_length=100) middle_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100)
...@@ -18,7 +18,8 @@ class WidgetUser(models.Model): ...@@ -18,7 +18,8 @@ class WidgetUser(models.Model):
department = models.ForeignKey(Department, on_delete=models.CASCADE, default=1) department = models.ForeignKey(Department, on_delete=models.CASCADE, default=1)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('widget_user', args=[(self.full_name)]) return reverse('widget_user-detail', args=[str(self.id_num)])
# return reverse('widget_user', args=[(self.full_name)])
def full_name(self): def full_name(self):
return '{} {} {}'.format(self.first_name, self.middle_name, self.last_name) return '{}, {} {}'.format(self.first_name, self.middle_name, self.last_name)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Detail</title>
</head>
<body>
<p>{{user.last_name}},{{user.first_name}} {{user.middle_name}}</p>
<p>{{user.id_num}}</p>
<p>{{user.email}}</p>
<p>{{user.department.dept_name}}</p>
<p>{{user.department.home_unit}}</p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User List</title>
</head>
<body>
<h1>Welcome to Widget!</h1>
{% for user in object_list %}
<a href="{{ user.get_absolute_url }}"> {{ user.last_name }}, {{user.first_name}} {{user.middle_name}} </a> <br>
{% endfor %}
</body>
</html>
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import homepage from .views import homepage, UserDetailView, UserListView
# urlpatterns = [
# path('', homepage, name='homepage')
# ]
urlpatterns = [ urlpatterns = [
path('', homepage, name='homepage') path('', UserListView.as_view(), name='widget_user-list'),
path('<int:pk>/details', UserDetailView, name='widget_user-detail'),
] ]
\ No newline at end of file
from django.http import HttpResponse from django.shortcuts import render, get_object_or_404
from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from .models import WidgetUser, Department from .models import WidgetUser, Department
def homepage(request): # def homepage(request):
widget_users = WidgetUser.objects.all() # widget_users = WidgetUser.objects.all()
output = "WIDGET USERS:\n" + "\n".join( # # output = "WIDGET USERS:\n" + "\n".join(
['{}, {} {}: {}, {}, {}'.format(str(user.last_name), # # ['{}, {} {}: {}, {}, {}'.format(str(user.last_name),
str(user.first_name), # # str(user.first_name),
str(user.middle_name), # # str(user.middle_name),
str(user.id_num), # # str(user.id_num),
str(user.email), # # str(user.email),
str(user.department)) # # str(user.department))
for user in widget_users] # # for user in widget_users]
) # # )
return HttpResponse(output, content_type="text/plain") # context = {
\ No newline at end of file # 'widget_users': widget_users,
# }
# # return HttpResponse(output, content_type="text/plain")
# return render(request, 'homepage/homepage.html', context)
class homepage(View):
def get(self, request):
widget_users = WidgetUser.objects.all()
context = {
'widget_users': widget_users,
}
return render(request, 'homepage/homepage.html', context)
# class UserDetailView(DetailView):
# model = WidgetUser
# With this configuration, the default template this renders
# is called user_detail.html
def UserDetailView(request, pk):
user = get_object_or_404(WidgetUser, pk=pk)
return render(request, 'homepage/widgetuser_detail.html', {
'user': user
})
class UserListView(ListView):
model = WidgetUser
# With this configuration, the default template this renders
# is called user_list.html
\ No newline at end of file
...@@ -21,6 +21,7 @@ urlpatterns = [ ...@@ -21,6 +21,7 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', include('homepage.urls'), name='Homepage'), path('', include('homepage.urls'), name='Homepage'),
path('homepage/', include('homepage.urls'), name='Homepage'), path('homepage/', include('homepage.urls'), name='Homepage'),
path('users/', include('homepage.urls'), name='Homepage'),
path('forum/', include('forum.urls'), name='Forum'), path('forum/', include('forum.urls'), name='Forum'),
path('assignments/', include('assignments.urls'), name='Assignments'), path('assignments/', include('assignments.urls'), name='Assignments'),
path('announcements/', include('announcements.urls'), name='Announcement Board'), path('announcements/', include('announcements.urls'), name='Announcement Board'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment