It's been a while since previous post, but I'll continue.
Actually, the work itself has proceeded quite freely and it seems that I was not just posting, so from this time on, let's take a small amount of them.
First of all, lightly, from the processing of the authentication system. Implemented by referring to Past posts I did.
owner/views.py
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
def log_in(req):
if req.user.is_authenticated():
return redirect('/') #If authenticated, redirect to the top page (not touched this time)
error_msg = "" #Define an error message and bring it to the template
if req.method == 'POST':
posted = req.POST
name,password = posted['name'],posted['password']
user = authenticate(username=name,password=password)
if user is not None:
if user.is_active:
login(req,user)
return redirect('/')
else:
error_msg = "This account is not active..."
else:
error_msg = "Log in failed...."
return render(req,'owner/log_in.html',{'error_msg':error_msg})
def log_out(req):
logout(req)
return redirect('/login/') #After logging out, return to the login screen
def new_owner(req): #A page to create a new User. I'm the owner of the shift
error_msg = ""
if req.method == 'POST':
posted = req.POST
name,password = posted['name'],posted['password']
if password != posted['pass_check']: #Check your password by typing twice as is often the case
error_msg = "Password check is failed!"
else:
if len(password) < 6: #Password must be at least 6 characters
error_msg = "Password need over 6 letters!"
else:
from django.db import IntegrityError
from django.contrib.auth.models import User
try:
owner = User.objects.create_user(username=name,password=password)
return redirect('/')
except IntegrityError: #If the user name is already used, this error will be thrown, so take measures
error_msg = "You can not use this name!"
return render(req,'owner/new_owner.html',{'error_msg':error_msg,})
@login_required #A convenient decorator for logging in (LOGIN in settings)_You need to set the URL. After that, you can put your login page at that URL (OK)
def edit_groupschedule(req): #It feels like deciding the shift setting, so it may be easier to understand if you use shift setting
from django.contrib.auth.models import Group
from owner.models import GroupSchedule
try: #If the accessing user does not have a Group Schedule yet, create a new one, if it already has one, edit it.
groupschedule = GroupSchedule.objects.get(owner=req.user)
group = groupschedule.group
except GroupSchedule.DoesNotExist:
groupschedule = GroupSchedule(owner=req.user)
group = Group()
if req.method == 'POST':
posted = req.POST
group.name = posted['name']
group.save()
groupschedule.group = group
groupschedule.start_point = int(posted['start_point'])
groupschedule.save()
return redirect('/') #Jump to the top after saving
return render(req,'owner/edit_groupschedule.html',{
'group_name':group.name,
'start_point':groupschedule.start_point,
'select_choices':(1,5,10,15,20,25,), #Candidates for monthly shift start dates
})
Next is the template. I'm using Bootstrap, but it's a bit annoying to write `<div>`
every time I make a form part, so the parts that are likely to be used repeatedly are ** templates / app ** Create a directory called ** parts ** below. You can then use the ** include ** template tag to pull it in. Is the image that ** extends is the foundation and include is the part **?
For example, like this.
parts/input.html
<div class="form-group">
<label>{{ label }}
<input type="{{ type }}" name="{{ name }}" class="form-control {{ class }}" value="{{ value }}">
</label>
</div>
The rest should be made appropriately. Since you can put a value in each variable when reading a part, you can also add classes. In addition, I made a part for the application based on this, so I will use it.
templates/owner/log_in.html
{% extends 'bases/base_form.html' %}
{% block title %}Log in{% endblock %}
{% block form %}
<h1>Log in<small>{{ error_msg }}</small></h1> <!--Message display if there is an error-->
{% include 'owner/parts/name_input.html' %}
{% include 'owner/parts/pass_input.html' %}
{% include 'parts/submit.html' with class='btn-lg btn-primary' value='Log in!' %} {#You can put a value in a variable with with#}
<a href="/owner/new">Create New User</a>
{% endblock form %}
The rest is almost the same.
..../new_user.html
{% extends 'owner/bases/base_form.html' %}
{% block title %}new Owner!{% endblock %}
{% block form %}
<h1>new Owner!<small>{{ error_msg }}</small></h1>
{% include 'owner/parts/name_input.html' %}
{% include 'owner/parts/pass_input.html' %}
{% include 'owner/parts/pass_input2.html' %}
{% include 'parts/submit.html' with value="Create new owner!" %}
{% endblock form %}
..../edit_groupschedule.html
{% extends 'owner/bases/base_form.html' %}
{% block title %}new Schedule!{% endblock %}
{% block form %}
{% include 'owner/parts/name_input.html' with value=group_name %} {#initial value. views.Feeling to pass the variables brought from py to the parts#}
{% include 'owner/parts/select_number.html' with start_point=start_point select_choices=select_choices %} {#Candidates for selection#}
{% include 'owner/parts/submit.html' with value='Create your Schedule!' %}
{% endblock form %}
It doesn't matter if it doesn't matter, but when I left a blank, I realized how difficult it was to understand my code (some classes and function names don't make sense). It's embarrassing, so I don't feel like posting so much, and if I think "OK" and proceed on my own, it will become more and more messy. I'll post up to the point where I did it for the time being, but I feel that it would be better to recreate it from scratch.
Recommended Posts