settings
+ change data (username, name, email)
This commit is contained in:
parent
05ee124c6d
commit
ed52d5c8c4
13 changed files with 130 additions and 4 deletions
0
invsystem/settings_app/__init__.py
Normal file
0
invsystem/settings_app/__init__.py
Normal file
3
invsystem/settings_app/admin.py
Normal file
3
invsystem/settings_app/admin.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
invsystem/settings_app/apps.py
Normal file
5
invsystem/settings_app/apps.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class SettingsAppConfig(AppConfig):
|
||||
name = 'settings_app'
|
||||
13
invsystem/settings_app/forms.py
Normal file
13
invsystem/settings_app/forms.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from django.forms import CharField,Form
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
class SettingsForm(Form):
|
||||
username = CharField(required=False)
|
||||
last_name = CharField(required=True)
|
||||
first_name = CharField(required=True)
|
||||
email = CharField(required=True)
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('username', 'first_name', 'last_name', 'email')
|
||||
3
invsystem/settings_app/models.py
Normal file
3
invsystem/settings_app/models.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
invsystem/settings_app/tests.py
Normal file
3
invsystem/settings_app/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
6
invsystem/settings_app/urls.py
Normal file
6
invsystem/settings_app/urls.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='settings_index'),
|
||||
]
|
||||
42
invsystem/settings_app/views.py
Normal file
42
invsystem/settings_app/views.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from django.shortcuts import render
|
||||
from .forms import SettingsForm
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
@login_required
|
||||
def index(request):
|
||||
if request.method == 'GET':
|
||||
user = request.user
|
||||
default = {'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name,
|
||||
'email': user.email}
|
||||
form = SettingsForm(initial=default)
|
||||
context = {'title': 'Einstellungen', 'form': form}
|
||||
return render(request, 'settings_app/settings.html', context)
|
||||
|
||||
elif request.method == 'POST':
|
||||
user = User.objects.get(username=request.user.username)
|
||||
form = SettingsForm(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
user.username = form.cleaned_data.get('username')
|
||||
user.first_name = form.cleaned_data.get('first_name')
|
||||
user.last_name = form.cleaned_data.get('last_name')
|
||||
user.email = form.cleaned_data.get('email')
|
||||
user.save()
|
||||
|
||||
default = {'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name,
|
||||
'email': user.email}
|
||||
form = SettingsForm(initial=default)
|
||||
context = {'title': 'Einstellungen', 'form': form, 'edited': True}
|
||||
return render(request, 'settings_app/settings.html', context)
|
||||
|
||||
else:
|
||||
default = {'username': user.username, 'first_name': user.first_name, 'last_name': user.last_name,
|
||||
'email': user.email}
|
||||
form = SettingsForm(initial=default)
|
||||
context = {'title': 'Einstellungen', 'form': form}
|
||||
return render(request, 'settings_app/settings.html', context)
|
||||
Loading…
Add table
Add a link
Reference in a new issue