Commit a1385349 authored by Justin Daniel Mendoza's avatar Justin Daniel Mendoza

Added homepage user adding functionality and new html template

parent f5d14595
No preview for this file type
from django.forms import ModelForm, ModelChoiceField
from .models import WidgetUser, Department
class UserForm(ModelForm):
class Meta:
model = WidgetUser
fields = ['last_name', 'first_name', 'middle_name', 'email', 'id_num','department','user_imageUrl']
# Generated by Django 4.0.3 on 2022-05-25 15:57
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0004_alter_widgetuser_user_imageurl'),
]
operations = [
migrations.RemoveField(
model_name='department',
name='widgetuser',
),
migrations.AddField(
model_name='widgetuser',
name='department',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.department'),
),
migrations.AlterField(
model_name='department',
name='dept_name',
field=models.CharField(max_length=50, null=True),
),
migrations.AlterField(
model_name='department',
name='home_unit',
field=models.CharField(max_length=50, null=True),
),
migrations.AlterField(
model_name='widgetuser',
name='email',
field=models.EmailField(max_length=100, null=True),
),
migrations.AlterField(
model_name='widgetuser',
name='first_name',
field=models.CharField(max_length=20, null=True),
),
migrations.AlterField(
model_name='widgetuser',
name='id_num',
field=models.IntegerField(null=True),
),
migrations.AlterField(
model_name='widgetuser',
name='last_name',
field=models.CharField(max_length=20, null=True),
),
migrations.AlterField(
model_name='widgetuser',
name='middle_name',
field=models.CharField(max_length=10, null=True),
),
migrations.AlterField(
model_name='widgetuser',
name='user_imageUrl',
field=models.CharField(default=1, max_length=999, null=True),
),
]
...@@ -2,19 +2,16 @@ from itertools import chain ...@@ -2,19 +2,16 @@ from itertools import chain
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class WidgetUser(models.Model):
first_name = models.CharField(max_length=20)
middle_name = models.CharField(max_length=10)
last_name = models.CharField(max_length=20)
id_num = models.IntegerField()
email = models.EmailField(max_length=100)
user_imageUrl = models.CharField(max_length=999, default = 1)
class Department(models.Model): class Department(models.Model):
widgetuser = models.ForeignKey(WidgetUser, on_delete=models.CASCADE) dept_name = models.CharField(max_length=50,null=True)
dept_name = models.CharField(max_length=50) home_unit = models.CharField(max_length=50,null=True)
home_unit = models.CharField(max_length=50)
class WidgetUser(models.Model):
first_name = models.CharField(max_length=20,null=True)
middle_name = models.CharField(max_length=10,null=True)
last_name = models.CharField(max_length=20,null=True)
id_num = models.IntegerField(null=True)
email = models.EmailField(max_length=100,null=True)
user_imageUrl = models.CharField(max_length=999, default = 1,null=True)
department = models.ForeignKey(Department, on_delete=models.CASCADE,null=True)
\ No newline at end of file
{% extends "homepage/base.html" %}
{% block content %}
<h1> Add Widget User!</h1>
<p>Please enter details for new widget user: </p>
<form method="POST" action= "{% url 'add' %}">
{% csrf_token %}
<div class="fieldWrapper">
<p>Enter a First Name:</p><br>
{{user_form.first_name}}
</div>
<div class="fieldWrapper">
<p>Enter a Middle name:</p><br>
{{user_form.middle_name}}
</div>
<div class="fieldWrapper">
<p>Enter a Last Name</p><br>
{{user_form.last_name}}
</div>
<div class="fieldWrapper">
<p>Enter an email address</p><br>
{{user_form.email}}
</div>
<div class="fieldWrapper">
<p>Enter an id number</p><br>
{{user_form.id_num}}
</div>
<div class="fieldWrapper">
<p>Choose a Department</p><br>
{{user_form.department}}
</div>
<div class="fieldWrapper">
<p>Enter a Image Url</p><br>
{{user_form.user_imageUrl}}
</div>
<br>
<button class="button" type="submit">Save New User</button>
</form>
<p><a href ="{% url 'homepage'%}">Click Here to go back to homepage</a></p>
{% endblock %}
\ No newline at end of file
...@@ -3,13 +3,10 @@ ...@@ -3,13 +3,10 @@
{% block content %} {% block content %}
<img src = {{user.user_imageUrl}}><br> <img src = {{user.user_imageUrl}}><br>
{% for dept in dept_list%} <h3>{{user.id_num}}</h3><br>
{% if dept.widgetuser.last_name == user.last_name %} <h3>{{user.email}}</h3><br>
<h3>{{dept.widgetuser.id_num}}</h3><br> <h3>{{user.department.dept_name}}</h3><br>
<h3>{{dept.widgetuser.email}}</h3><br> <h3>{{user.department.home_unit}}</h3><br>
<h3>{{dept.dept_name}}</h3><br>
<h3>{{dept.home_unit}}</h3><br>
{% endif %}
{% endfor %}
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<p><li><a href ="{% url 'user' user.id%}">{{user.last_name}}, {{user.first_name}} {{user.middle_name}}</a></li></p> <p><li><a href ="{% url 'user' user.id%}">{{user.last_name}}, {{user.first_name}} {{user.middle_name}}</a></li></p>
{% endfor %} {% endfor %}
</ol> </ol>
<p><a href ="{% url 'add'%}">Click Here to add new user</a></p>
{% else %} {% else %}
<p> users do not exist. </p> <p> users do not exist. </p>
{% endif %} {% endif %}
......
from django.urls import path from django.urls import path
from .views import HomePageView
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', HomePageView.as_view(), name='views_homepage'),
path("<int:user_id>/details/", views.user, name='user') path("users/<int:user_id>/details/", views.user, name='user'),
path("users/add", views.add, name='add'),
path("", views.add, name='homepage')
] ]
\ No newline at end of file
from django.http import HttpResponse from django.http import HttpResponse
import homepage
from .forms import UserForm
from . models import WidgetUser from . models import WidgetUser
from . models import Department from . models import Department
from django.shortcuts import render from django.shortcuts import render, redirect
from django.views import View
from django.views.generic.detail import DetailView
# Create your views here. # Create your views here.
def index(request): class HomePageView(View):
def get(self, request):
widgets = WidgetUser.objects.all() widgets = WidgetUser.objects.all()
depts = Department.objects.all() depts = Department.objects.all()
user_list = WidgetUser.objects.order_by("last_name") user_list = WidgetUser.objects.order_by("last_name")
...@@ -13,13 +19,24 @@ def index(request): ...@@ -13,13 +19,24 @@ def index(request):
'Depts':depts, 'Depts':depts,
'user_list':user_list 'user_list':user_list
} }
return render(request, "homepage/view_homepage.html", context) return render(request, "homepage/view_homepage.html", context)
def user(request, user_id): def user(request, user_id):
dept_list = Department.objects.order_by("widgetuser_id") dept_list = WidgetUser.objects.order_by("first_name")
try: try:
user = WidgetUser.objects.get(pk=user_id) user = WidgetUser.objects.get(pk=user_id)
except WidgetUser.DoesNotExist: except WidgetUser.DoesNotExist:
raise Http404("User doesnt exist") raise Http404("User doesnt exist")
return render(request,"homepage/user_details.html",{"user": user, 'dept_list':dept_list}) return render(request,"homepage/user_details.html",{"user": user, 'dept_list':dept_list})
def add(request):
user_form = UserForm()
if request.method == "POST":
user_form = UserForm(request.POST)
if user_form.is_valid():
user_form.save()
return redirect("homepage")
else:
user_form = UserForm()
return render(request, "homepage/user_add.html", {"user_form": user_form})
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