Django User Profile
Free Django samples that allows registered users to edit their profile outside of the admin module.
Django Sample project that allows registered users to edit their profile outside of the admin module. Django User Profile is provided on top of Django Volt - Features:
    UI Kit: Volt Bootstrap 5 by Themesberg
    UI-Ready App, SQLite Database, Django Native ORM
    Session-Based Authentication, Forms validation
    Editable user profile: Name, Surname, Email, Phone, and address
    User Profile Page: settings.html
Links
Django User Profile - Free Sample.
​

How to use the app

To compile and execute the project in a local environment the workstation must have a few software tools already installed:
    ​GIT - command line versioning tool used to clone the sources
    ​Python3 - the language used to code the project
Check GIT is installed
1
$ git -v
2
git version 2.28.0.windows.1 <-- All Good
Copied!
Check Python is installed
1
python --version
2
Python 3.8.4 <-- All Good
Copied!
Once we have this minimal toolchain properly installed and accessible via a terminal window we can move forward and compile the code.
Step #1 - Clone/download sources from the public repository ​
1
$ git clone https://github.com/app-generator/django-user-profile.git
2
$ cd django-user-profile
Copied!
Step #2 - Install modules using a Virtual Environment
1
$ virtualenv env
2
$ source env/bin/activate
3
$
4
$ pip3 install -r requirements.txt
Copied!
Step #3 - Create SQLite Database and tables
1
$ python manage.py makemigrations
2
$ python manage.py migrate
Copied!
​

Codebase Structure

1
< PROJECT ROOT >
2
|
3
|-- core/ # Implements app logic and serve the static assets
4
| |-- settings.py # Django app bootstrapper
5
| |-- static/
6
| |-- templates/ # Templates used to render pages
7
|
8
|-- authentication/ # Handles auth routes (login and register)
9
| |-- urls.py # Define authentication routes
10
| |-- forms.py # Define auth forms
11
|
12
|-- app/ # A simple app that serve HTML files
13
| |-- views.py # Serve HTML pages for authenticated users
14
| |-- urls.py # Define some super simple routes
15
|
16
|-- customers/ # Handles the profile edit <-------- NEW
17
| |-- __init__.py # Defines App init <-------- NEW
18
| |-- admin.py # Defines App admin <-------- NEW
19
| |-- apps.py # Defines App apps <-------- NEW
20
| |-- forms.py # Defines App forms <-------- NEW
21
| |-- models.py # Defines App models <-------- NEW
22
| |-- signals.py # Defines App signals <-------- NEW
23
| |-- tests.py # Defines App tests <-------- NEW
24
| |-- urls.py # Defines App routes <-------- NEW
25
| |-- views.py # Defines App views <-------- NEW
26
|
27
|-- requirements.txt # Development modules - SQLite storage
28
|-- .env # Inject Configuration via Environment
29
|-- manage.py # Start the app - Django default start script
30
|
31
|-- ***********************************
Copied!
​
The bootstrap flow
    Django bootstrapper manage.py uses core/settings.py as the main configuration file
    core/settings.py loads the app magic from .env file
    Redirect the guest users to Login page
    Unlock the pages served by app node for authenticated users

User Profile Feature

This section describes the coding process for this feature that allows authenticated users to update their profiles. By accessing the settings.html page, registered users can update their own profile: name, surname, phone number, city name, ZIP code, ..etc.
The customers app:
This module will manage the user profile information by defining a new model, form, and view. Authenticated users can also upload their avatar.
The Profile model:
customers/models.py:
1
class Profile(models.Model):
2
​
3
# Managed fields
4
user = models.OneToOneField(User, related_name="profile", on_delete=models.CASCADE)
5
avatar = models.ImageField(upload_to="customers/profiles/avatars/", null=True, blank=True)
6
birthday = models.DateField(null=True, blank=True)
7
gender = models.PositiveSmallIntegerField(choices=GENDER_CHOICES, null=True, blank=True)
8
phone = models.CharField(max_length=32, null=True, blank=True)
9
address = models.CharField(max_length=255, null=True, blank=True)
10
number = models.CharField(max_length=32, null=True, blank=True)
11
city = models.CharField(max_length=50, null=True, blank=True)
12
zip = models.CharField(max_length=30, null=True, blank=True)
Copied!

The Profile form

Create a related form to show inputs & store data. The ProfileForm will be defined in the customers/forms.py using a definition as below:
1
from django import forms
2
from customers.models import Profile
3
​
4
class ProfileForm(forms.ModelForm):
5
first_name = forms.CharField(max_length=255)
6
last_name = forms.CharField(max_length=255)
7
email = forms.EmailField()
8
​
9
class Meta:
10
model = Profile
11
fields = '__all__'
12
exclude = ['user']
Copied!

The Profile routing

Activate the routing for customers app by edit the customers/urls.py file with the following code:
1
from django.urls import path
2
from customers import views
3
​
4
urlpatterns = [
5
path('profile/', views.ProfileView.as_view(), name='profile'),
6
]
Copied!
Update core/urls to include customers urls:
1
from django.conf import settings
2
from django.contrib import admin
3
from django.conf.urls.static import static
4
from django.urls import path, include
5
​
6
urlpatterns = [
7
path('admin/', admin.site.urls), # Django admin route
8
path('customers/', include("customers.urls")), # Django customers route
9
# ...
10
]
11
​
12
# to support and show media & static files in developer mode
13
if settings.DEVEL:
14
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
15
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Copied!
​
Links & Resources:
Last modified 5mo ago