Commit 94be781c authored by Pierre Ashley Salcedo's avatar Pierre Ashley Salcedo

feat: added form for creating new entry in WidgetUser

parent 98c380aa
from django.forms import ModelForm
from .models import WidgetUser
class UserForm(ModelForm):
class Meta:
model = WidgetUser
fields = ["last_name", "first_name", "middle_name", "id_num", "email", "department", "profile_image"]
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-05-26 21:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('homepage', '0002_auto_20220407_0505'),
]
operations = [
migrations.AddField(
model_name='widgetuser',
name='profile_image',
field=models.ImageField(blank=True, null=True, upload_to='images/'),
),
]
...@@ -26,6 +26,11 @@ class WidgetUser(models.Model): ...@@ -26,6 +26,11 @@ class WidgetUser(models.Model):
default=None, default=None,
null=True null=True
) )
profile_image = models.ImageField(
upload_to="images/",
blank=True,
null=True
)
department = models.ForeignKey( department = models.ForeignKey(
Department, Department,
on_delete=models.CASCADE, on_delete=models.CASCADE,
......
...@@ -31,16 +31,29 @@ ul { list-style-type: none; } ...@@ -31,16 +31,29 @@ ul { list-style-type: none; }
li { margin: 10px 0; } li { margin: 10px 0; }
table { width: 100%; }
th { text-align: right;}
a { color: rgb(255, 255, 255); } a { color: rgb(255, 255, 255); }
img { width: 400px; } img { width: 400px; }
button {
width: 10em;
margin: 1em 3em;
}
input, select {
width: 100%;
box-sizing: border-box;
}
#userInfo { #userInfo {
display: flex; display: flex;
flex-flow: row wrap-reverse; flex-flow: row wrap-reverse;
justify-content: space-evenly; justify-content: space-evenly;
} }
#userInfo > div { #userInfo > div { flex: 0 1 auto; }
flex: 0 1 auto;
}
\ No newline at end of file
{% extends "homepage/base.html" %}
{% block content %}
<header><h1> ADD WIDGET USER </h1></header>
<form action="{% url 'homepage:add' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<table>
{{ user_form.as_table }}
</table>
<br>
<button class="button" type="submit"> Save New User </button>
</form>
{% endblock %}
\ No newline at end of file
...@@ -14,15 +14,11 @@ ...@@ -14,15 +14,11 @@
</ul> </ul>
</div> </div>
<div> <div>
{% if user.id_num == 201234 %} {% if user.profile_image %}
<img src = "{% static 'homepage/images/201234.png' %}" alt = "profile image"> <img src = "{{user.profile_image.url}}" alt = "profile image">
{% elif user.id_num == 201524 %}
<img src = "{% static 'homepage/images/201524.png' %}" alt = "profile image">
{% elif user.id_num == 204483 %}
<img src = "{% static 'homepage/images/204483.png' %}" alt = "profile image">
{% else %} {% else %}
<img src = "{% static 'homepage/images/205678.png' %}" alt = "profile image"> <img src = "{% static 'homepage/images/no_image.png' %}">
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -7,12 +7,16 @@ ...@@ -7,12 +7,16 @@
{% if user_list %} {% if user_list %}
<ol> <ol>
{% for user in user_list %} {% for user in user_list %}
<li> <a href = "{% url 'homepage:detail' user.id_num %}"> <li><a href = "{% url 'homepage:detail' user.id_num %}">
{{user.last_name}}, {{user.first_name}} {{user.middle_name}} {{user.last_name}}, {{user.first_name}} {{user.middle_name}}
</a></li> </a></li>
{% endfor %} {% endfor %}
</ol> </ol>
<ul><a href = "{% url 'homepage:add' %}" class = "addToListLink">
Add Widget User
</a></ul>
{% else %} {% else %}
<p> No users available. </p> <p> No users available. </p>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -4,5 +4,6 @@ from . import views ...@@ -4,5 +4,6 @@ from . import views
app_name = "homepage" app_name = "homepage"
urlpatterns = [ urlpatterns = [
path("", views.index, name="homepage"), path("", views.index, name="homepage"),
path("<int:user_id>/details", views.detail, name="detail") path("users/add/", views.add, name="add"),
path("users/<int:user_id>/details/", views.detail, name="detail")
] ]
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import redirect, render
from django.http import Http404, HttpResponse from django.http import Http404, HttpResponse
from .models import WidgetUser from .models import WidgetUser
from .forms import UserForm
# Create your views here. # Create your views here.
def index(request): def index(request):
...@@ -18,4 +19,14 @@ def detail(request, user_id): ...@@ -18,4 +19,14 @@ def detail(request, user_id):
context = { context = {
"user": user "user": user
} }
return render(request, "homepage/detail.html", context) return render(request, "homepage/detail.html", context)
\ No newline at end of file
def add(request):
if request.method == "POST":
user_form = UserForm(request.POST, request.FILES)
if user_form.is_valid():
new_user = user_form.save()
return redirect("homepage:homepage")
else:
user_form = UserForm()
return render(request, "homepage/add.html", {"user_form": user_form})
\ No newline at end of file
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