Commit 9655250f authored by Jonathan Talbot's avatar Jonathan Talbot

homepage-final

parent 1f43cf10
No preview for this file type
from django import forms from django.forms import ModelForm
from .models import WidgetUser
class IndexCardForm(forms.Form): class WidgetUserForm(ModelForm):
name = forms.CharField(label='Full Name', max_length=100) class Meta:
section = forms.CharField(label='CSCI40 Section', max_length=5) model = WidgetUser
age = forms.IntegerField(label='Current Age') fields = ['last_name', 'first_name', 'middle_name', 'id_num', 'email', 'department'] # Selected fields for the form
\ No newline at end of file \ No newline at end of file
...@@ -12,5 +12,7 @@ ...@@ -12,5 +12,7 @@
<p>{{user.email}}</p> <p>{{user.email}}</p>
<p>{{user.department.dept_name}}</p> <p>{{user.department.dept_name}}</p>
<p>{{user.department.home_unit}}</p> <p>{{user.department.home_unit}}</p>
<hr>
<a href="{% url 'widget_user-list' %}">Back to Homepage</a>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -8,5 +8,13 @@ ...@@ -8,5 +8,13 @@
</head> </head>
<body> <body>
<h1>Add Widget User</h1> <h1>Add Widget User</h1>
<hr>
<form action="{% url 'widget_user-add' %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save WidgetUser">
</form>
<hr>
<a href="{% url 'widget_user-list' %}">Back to Homepage</a>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -8,10 +8,18 @@ ...@@ -8,10 +8,18 @@
</head> </head>
<body> <body>
<h1>Welcome to Widget!</h1> <h1>Welcome to Widget!</h1>
{% for user in object_list %} <hr>
<a href="{{ user.get_absolute_url }}"> {{ user.last_name }}, {{user.first_name}} {{user.middle_name}} </a> <br> <p>Widget Users:</p>
{% endfor %} {% if object_list %}
<ul>
<a href="/users/add">test</a> {% for user in object_list %}
<li><a href="{{ user.get_absolute_url }}"> {{ user.last_name }}, {{user.first_name}} {{user.middle_name}} </a></li><br>
{% endfor %}
</ul>
{% else %}
<p>There are no widget users.</p>
{% endif %}
<hr>
<a href="{% url 'widget_user-add' %}"><button>Add Widget User</button></a>
</body> </body>
</html> </html>
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import homepage, UserDetailView, UserListView from .views import UserDetailView, UserListView, addWidgetUser
urlpatterns = [ urlpatterns = [
path('', UserListView.as_view(), name='widget_user-list'), path('', UserListView.as_view(), name='widget_user-list'),
path('<int:pk>/details', UserDetailView, name='widget_user-detail'), path('<int:pk>/details', UserDetailView, name='widget_user-detail'),
path('add/', addWidgetUser, name='widget_user-add'),
] ]
\ No newline at end of file
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404, redirect
from django.views import View from django.views import View
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from .models import WidgetUser, Department from .models import WidgetUser, Department
from .forms import WidgetUserForm
class homepage(View):
def get(self, request):
widget_users = WidgetUser.objects.all()
context = {
'widget_users': widget_users,
}
return render(request, 'homepage/homepage.html', context)
def UserDetailView(request, pk): def UserDetailView(request, pk):
user = get_object_or_404(WidgetUser, pk=pk) user = get_object_or_404(WidgetUser, pk=pk)
...@@ -19,4 +13,18 @@ def UserDetailView(request, pk): ...@@ -19,4 +13,18 @@ def UserDetailView(request, pk):
}) })
class UserListView(ListView): class UserListView(ListView):
model = WidgetUser queryset = WidgetUser.objects.order_by('last_name') # Orders by last name alphabetically
\ No newline at end of file context_object_name = "post_list"
template_name = "post_list.html"
def addWidgetUser(request):
form = WidgetUserForm()
if request.method == 'POST':
form = WidgetUserForm(request.POST)
if form.is_valid():
new_widgetuser = form.save() # Creates new widget user
return redirect('widget_user-detail', pk=new_widgetuser.pk) # Redirects to detailed view of new widget user
else:
form = WidgetUserForm()
context = {'form':form}
return render(request,'homepage/widgetuser_form.html', context)
\ 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