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):
default=None,
null=True
)
profile_image = models.ImageField(
upload_to="images/",
blank=True,
null=True
)
department = models.ForeignKey(
Department,
on_delete=models.CASCADE,
......
......@@ -31,16 +31,29 @@ ul { list-style-type: none; }
li { margin: 10px 0; }
table { width: 100%; }
th { text-align: right;}
a { color: rgb(255, 255, 255); }
img { width: 400px; }
button {
width: 10em;
margin: 1em 3em;
}
input, select {
width: 100%;
box-sizing: border-box;
}
#userInfo {
display: flex;
flex-flow: row wrap-reverse;
justify-content: space-evenly;
}
#userInfo > div {
flex: 0 1 auto;
}
\ No newline at end of file
#userInfo > div { flex: 0 1 auto; }
{% 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,14 +14,10 @@
</ul>
</div>
<div>
{% if user.id_num == 201234 %}
<img src = "{% static 'homepage/images/201234.png' %}" 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">
{% if user.profile_image %}
<img src = "{{user.profile_image.url}}" alt = "profile image">
{% else %}
<img src = "{% static 'homepage/images/205678.png' %}" alt = "profile image">
<img src = "{% static 'homepage/images/no_image.png' %}">
{% endif %}
</div>
</div>
......
......@@ -7,12 +7,16 @@
{% if user_list %}
<ol>
{% 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}}
</a></li>
{% endfor %}
</ol>
<ul><a href = "{% url 'homepage:add' %}" class = "addToListLink">
Add Widget User
</a></ul>
{% else %}
<p> No users available. </p>
{% endif %}
{% endblock %}
\ No newline at end of file
......@@ -4,5 +4,6 @@ from . import views
app_name = "homepage"
urlpatterns = [
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 .models import WidgetUser
from .forms import UserForm
# Create your views here.
def index(request):
......@@ -19,3 +20,13 @@ def detail(request, user_id):
"user": user
}
return render(request, "homepage/detail.html", context)
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