New Feature: added createUser and newUser classes in homepage/views.py and...

New Feature: added createUser and newUser classes in homepage/views.py and created createUser_form.html to allow people to create their own users and save them in the database

Improvement: added foreign key to WidgetUser model in homepage instead of the Department model for ease of access of fields. Also edited urls.py
parent 6f223df6
# Generated by Django 3.2.12 on 2022-05-25 16:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('homepage', '0027_alter_widgetuser_profile_pic'),
]
operations = [
migrations.AlterField(
model_name='widgetuser',
name='profile_pic',
field=models.ImageField(blank=True, default='faceless.png', null=True, upload_to=''),
),
]
# Generated by Django 3.2.12 on 2022-05-25 16:34
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('homepage', '0028_alter_widgetuser_profile_pic'),
]
operations = [
migrations.AlterField(
model_name='widgetuser',
name='profile_pic',
field=models.ImageField(blank=True, default='static/faceless.png', null=True, upload_to=''),
),
]
# Generated by Django 3.2.12 on 2022-05-25 16:35
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('homepage', '0029_alter_widgetuser_profile_pic'),
]
operations = [
migrations.AlterField(
model_name='department',
name='dept_name',
field=models.CharField(default='none', max_length=50, null=True),
),
migrations.AlterField(
model_name='department',
name='home_unit',
field=models.CharField(default='none', max_length=50, null=True),
),
]
# Generated by Django 3.2.12 on 2022-05-25 16:43
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0030_auto_20220525_1935'),
]
operations = [
migrations.RemoveField(
model_name='department',
name='widgetuser',
),
migrations.AddField(
model_name='widgetuser',
name='department',
field=models.ForeignKey(default='1', on_delete=django.db.models.deletion.CASCADE, to='homepage.department'),
),
]
# Generated by Django 3.2.12 on 2022-05-25 17:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('homepage', '0031_auto_20220525_1943'),
]
operations = [
migrations.AlterField(
model_name='widgetuser',
name='profile_pic',
field=models.ImageField(blank=True, default='faceless.png', null=True, upload_to=''),
),
]
...@@ -2,21 +2,22 @@ from email.policy import default ...@@ -2,21 +2,22 @@ from email.policy import default
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class Department(models.Model):
dept_name = models.CharField(max_length=50, null=True, default='none')
home_unit = models.CharField(max_length=50, null=True, default='none')
def __str__(self):
return self.dept_name
class WidgetUser(models.Model): class WidgetUser(models.Model):
first_name = models.CharField(max_length=50, default='first_name') first_name = models.CharField(max_length=50, default='first_name')
middle_name = models.CharField(max_length=50, default='middle_name') middle_name = models.CharField(max_length=50, default='middle_name')
last_name = models.CharField(max_length=50, default='last_name') last_name = models.CharField(max_length=50, default='last_name')
id_num = models.CharField(max_length=7, default='1234567') id_num = models.CharField(max_length=7, default='1234567')
email = models.CharField(max_length=50, default='someemail@gmail.com') email = models.CharField(max_length=50, default='someemail@gmail.com')
profile_pic = models.ImageField(default="faceless_9ltCukV.png", null=True, blank=True) profile_pic = models.ImageField(default="faceless.png", null=True, blank=True)
department = models.ForeignKey(Department, on_delete=models.CASCADE, null=False, blank=False, default="1")
def __str__(self):
return self.last_name
class Department(models.Model):
widgetuser = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=False, blank=False)
dept_name = models.CharField(max_length=50, default='none')
home_unit = models.CharField(max_length=50, default='none')
def __str__(self): def __str__(self):
return self.dept_name return self.last_name
\ No newline at end of file \ No newline at end of file
...@@ -6,7 +6,8 @@ ...@@ -6,7 +6,8 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE-edge"> <meta http-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width-device-width, initial-scale=1.0"> <meta name="viewport" content="width-device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static 'homepage/style.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'style.css' %}">
<title> Homepage </title>
</head> </head>
<body> <body>
{% block content %} {% block content %}
......
{% extends "homepage/base.html" %}
{% block content %}
<form action="newUser/" method="POST">
{% csrf_token %}
<h3> Enter Last Name: </h3> <input type="text" name="Last name">
<h3> Enter First Name: </h3> <input type="text" name="First name">
<h3> Enter Middle Name: </h3> <input type="text" name="Middle name">
<h3> Enter ID Number: </h3> <input type="text" name="ID Number">
<h3> Enter Email address: </h3> <input type="text" name="Email address">
<!-- will change department name -->
<input type="Submit">
</form>
{% endblock %}
\ No newline at end of file
{% extends "homepage/base.html" %} {% extends "homepage/base.html" %}
{% block content %} {% block content %}
<h2> <h2>
{{ Department.widgetuser.last_name }}, {{ WidgetUser.last_name }},
{{ Department.widgetuser.first_name }} {{ WidgetUser.first_name }}
{{ Department.widgetuser.middle_name }} {{ WidgetUser.middle_name }}
</h2> </h2>
<ul> <ul>
<li>{{ Department.widgetuser.id_num }}</li> <li>{{ WidgetUser.id_num }}</li>
<li>{{ Department.widgetuser.email }}</li> <li>{{ WidgetUser.email }}</li>
<li>{{ Department.dept_name }}</li> <li>{{ WidgetUser.department.dept_name }}</li>
<li>{{ Department.home_unit }}</li> <li>{{ WidgetUser.department.home_unit }}</li>
</ul> </ul>
<br> <br>
<img src="{{ Department.widgetuser.profile_pic.url }}" width="20%"> <img src="{{ WidgetUser.profile_pic.url }}" width="20%">
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends "homepage/base.html" %} {% extends "homepage/base.html" %}
{% block content %} {% block content %}
<p> <p>
<h1>Welcome to Widget!</h1> <h1>Welcome to Widget!</h1>
<h2>WIDGET USERS:</h2> <h2>WIDGET USERS:</h2>
{% if WidgetUser %} {% if WidgetUser %}
<ol> <ol>
{% for widgetUser in WidgetUser %} {% for widgetUser in WidgetUser %}
<li> <li>
<a href="{% url 'homepage:details' widgetUser.id_num %}"> <a href="{% url 'homepage:details' widgetUser.id %}">
{{ widgetUser.last_name }}, {{ widgetUser.last_name }},
{{ widgetUser.first_name }} {{ widgetUser.first_name }}
{{ widgetUser.middle_name }} {{ widgetUser.middle_name }}
</a> </a>
</li> </li>
{% endfor %} {% endfor %}
</ol> </ol>
{% else %} {% else %}
<p>No widget users registered.</p> <h1>No widget users registered.</h1>
{% endif %} {% endif %}
</p>
<a href="{% url 'homepage:createUser' %}"> Add Widget User </a>
</p>
{% endblock %} {% endblock %}
...@@ -5,6 +5,8 @@ from . import views ...@@ -5,6 +5,8 @@ from . import views
app_name = "homepage" app_name = "homepage"
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('users/add/newUser/', views.newUser, name='newUser'),
path('users/add/', views.createUser, name='createUser'),
path("users/<int:widgetuser_id>/details/", views.detail, name='details') path("users/<int:widgetuser_id>/details/", views.detail, name='details')
] ]
\ No newline at end of file
from tkinter import Widget
from .models import Department from .models import Department
from .models import WidgetUser from .models import WidgetUser
from django.http import Http404 from django.http import HttpResponse, Http404
from django.shortcuts import render from django.shortcuts import render
widgetUser = WidgetUser.objects.order_by("last_name")
def index(request): def index(request):
widgetUser = WidgetUser.objects.order_by("last_name")
context = { context = {
"WidgetUser": widgetUser "WidgetUser": widgetUser
} }
return render(request, "homepage/widgetusers_orderedlist.html", context) return render(request, "homepage/widgetusers_orderedlist.html", context)
def createUser(request):
return render(request, "homepage/createUser_form.html")
def newUser(request):
if request.method == "POST":
first_name = request.POST.get("First name")
middle_name = request.POST.get("Middle name")
last_name = request.POST.get("Last name")
id_num = request.POST.get("ID Number")
email = request.POST.get("Email address")
# will add department
widgetUser = WidgetUser.objects.create(first_name=first_name, middle_name=middle_name, last_name=last_name, id_num=id_num, email=email)
return HttpResponse("User Added")
def detail(request, widgetuser_id): def detail(request, widgetuser_id):
# widgetUser = WidgetUser.objects.get(pk=widgetuser_id)
# if not widgetUser.exists():
# raise Http404("User does not exist!")
try: try:
department = Department.objects.get(pk=widgetuser_id) widgetUser = WidgetUser.objects.get(pk=widgetuser_id)
except Department.DoesNotExist: except WidgetUser.DoesNotExist:
raise Http404("User does not exist!") raise Http404("User does not exist!")
return render(request, "homepage/details.html", {"Department": department}) return render(request, "homepage/details.html", {"WidgetUser": widgetUser})
\ No newline at end of file \ No newline at end of file
...@@ -6,6 +6,10 @@ h2 { ...@@ -6,6 +6,10 @@ h2 {
color: palevioletred; color: palevioletred;
font-weight: bold; font-weight: bold;
} }
h3 {
color: palevioletred;
font-weight: bold;
}
ol { ol {
color: wheat; color: wheat;
font-weight: bold; font-weight: bold;
......
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