Commit 39e509fa authored by Almira Redoble's avatar Almira Redoble

Implemented add new widget user page. Firstly, created forms.py and wrote...

Implemented add new widget user page. Firstly, created forms.py and wrote class UserForm so that css styling can be applied to the form. Forms.py was imported into views.py so that UserForm may be passed into the form_class attribute of the newly created CreateView, UserCreateView. This view was then linked to widgetuser-add.html, which was created afterwards. The appropriate path was added to urls.py.
parent b694ca00
from django import forms
from .models import WidgetUser
class UserForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
for visible in self.visible_fields():
visible.field.widget.attrs['class'] = 'form-design'
class Meta:
model = WidgetUser
fields = '__all__'
labels = {
'department': ('Department, Home Unit'),
}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Widget User{% endblock %}
{% block heading %}Add a new Widget User:{% endblock %}
{% block content %}
{{ form.non_field_errors }}
<form action="{% url 'dashboard:user-add' %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Widget User" class="btn">
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index, UserDetailView from .views import index, UserDetailView, UserCreateView
urlpatterns = [ urlpatterns = [
path('dashboard/', index, name='index'), path('dashboard/', index, name='index'),
path('widgetusers/<int:pk>/details/', UserDetailView.as_view(), name='user-details'), path('widgetusers/<int:pk>/details/', UserDetailView.as_view(), name='user-details'),
path('widgetusers/add/', UserCreateView.as_view(), name='user-add'),
] ]
......
from django.shortcuts import render from django.shortcuts import render
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import WidgetUser from .models import WidgetUser
from .forms import UserForm
def index(request): def index(request):
...@@ -11,3 +13,8 @@ def index(request): ...@@ -11,3 +13,8 @@ def index(request):
class UserDetailView(DetailView): class UserDetailView(DetailView):
model = WidgetUser model = WidgetUser
template_name = 'dashboard/widgetuser-details.html' template_name = 'dashboard/widgetuser-details.html'
class UserCreateView(CreateView):
model = WidgetUser
form_class = UserForm
template_name = 'dashboard/widgetuser-add.html'
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