Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Django User Profile - Open-source Django Sample | AppSeed

License

NotificationsYou must be signed in to change notification settings

app-generator/sample-django-extended-user-profile

Repository files navigation

Django Sample project that allows registered users to edit their profile outside of the admin module.Django User Profile is provided on top ofVolt, a popular open-sourceDjango Bootstrap 5 provided byThemesberg andAppSeed. For newcomers,Django is the most popular Python-based web framework initially released in 2003 and is currently a top-rated framework in web development.



For acomplete set of features and long-term support, check outDynamic Django, a powerful starter that incorporates:

  • Dynamic DataTables: using a single line of configuration, the data saved in any table is automatically managed
  • Dynamic API: any model can become a secure API Endpoint using DRF
  • Dynamic Charts: extract relevant charts without coding all major types are supported
  • CSV Loader: translate CSV files into Django Models and (optional) load the information
  • ✅ PowerfulCLI Tools for the GIT interface, configuration editing, updating the configuration and database (create models, migrate DB)

How to use it

$# Get the code$ git clone https://github.com/app-generator/sample-django-extended-user-profile.git$cd sample-django-extended-user-profile$$# Virtualenv modules installation (Unix based systems)$ virtualenv env$source env/bin/activate$$# Virtualenv modules installation (Windows based systems)$# virtualenv env$# .\env\Scripts\activate$$# Install modules - SQLite Storage$ pip3 install -r requirements.txt$$# Create tables$ python manage.py makemigrations$ python manage.py migrate$$# Start the application (development mode)$ python manage.py runserver# default port 8000$$# Start the app - custom port$# python manage.py runserver 0.0.0.0:<your_port>$$# Access the web app in browser: http://127.0.0.1:8000/

Note: To use the app, please access the registration page and create a new user. After authentication, the app will unlock the private pages.


Codebase structure

The project is coded using a simple and intuitive structure presented below:

< PROJECT ROOT>||-- core/# Implements app logic and serve the static assets||-- settings.py# Django app bootstrapper||-- static/|||--<css, JS, images># CSS files, Javascripts files||-- templates/# Templates used to render pages||||-- includes/# HTML chunks and components||-- layouts/# Master pages||-- accounts/# Authentication pages|||      index.html# The default page|*.html# All other HTML pages||-- authentication/# Handles auth routes (login and register)||-- urls.py# Define authentication routes||-- forms.py# Define auth forms||-- app/# A simple app that serve HTML files||-- views.py# Serve HTML pages for authenticated users||-- urls.py# Define some super simple routes||-- customers/# Handles the profile edit     <-------- NEW||-- __init__.py# Defines App init             <-------- NEW||-- admin.py# Defines App admin            <-------- NEW||-- apps.py# Defines App apps             <-------- NEW||-- forms.py# Defines App forms            <-------- NEW||-- models.py# Defines App models           <-------- NEW||-- signals.py# Defines App signals          <-------- NEW||-- tests.py# Defines App tests            <-------- NEW||-- urls.py# Defines App routes           <-------- NEW||-- views.py# Defines App views            <-------- NEW||-- requirements.txt# Development modules - SQLite storage|-- .env# Inject Configuration via Environment|-- manage.py# Start the app - Django default start script||--************************************************************************

The bootstrap flow

  • Django bootstrappermanage.py usescore/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 byapp node for authenticated users

User Profile Feature

This section describes the coding process for this feature that allows authenticated users to update their profiles.

Settings screenshot


Settings

In this section, the user can change profile information, including name, email, avatar, ... etc.To do this, we create a new app calledcustomers, then create a model namedProfile to store user information.

Thecustomers app:

This module will manage the user profile information by defining a new model, form, and view. Authenticated users can also upload theiravatar.

$ python manage.py startapp customers

TheProfile model

customers/models.py:

classProfile(models.Model):# Managed fieldsuser=models.OneToOneField(User,related_name="profile",on_delete=models.CASCADE)avatar=models.ImageField(upload_to="customers/profiles/avatars/",null=True,blank=True)birthday=models.DateField(null=True,blank=True)gender=models.PositiveSmallIntegerField(choices=GENDER_CHOICES,null=True,blank=True)phone=models.CharField(max_length=32,null=True,blank=True)address=models.CharField(max_length=255,null=True,blank=True)number=models.CharField(max_length=32,null=True,blank=True)city=models.CharField(max_length=50,null=True,blank=True)zip=models.CharField(max_length=30,null=True,blank=True)@propertydefget_avatar(self):returnself.avatar.urlifself.avatarelsestatic('assets/img/team/default-profile-picture.png')

get_avatar: In this property, if the avatar value is not provided by the user, the default value is used.


TheProfile form

Create related form to show inputs & store data. TheProfileForm will be defined in thecustomers/forms.py using a definition as below:

fromdjangoimportformsfromcustomers.modelsimportProfileclassProfileForm(forms.ModelForm):first_name=forms.CharField(max_length=255)last_name=forms.CharField(max_length=255)email=forms.EmailField()classMeta:model=Profilefields='__all__'exclude= ['user']defform_validation_error(form):"""    Form Validation Error    If any error happened in your form, this function returns the error message.    """msg=""forfieldinform:forerrorinfield.errors:msg+="%s: %s\\n"% (field.labelifhasattr(field,'label')else'Error',error)returnmsg

Note: We have three fields (first_name, last_name, email) that are outside the profile model (they are in the user model). We need to add these three fields to our form.

form_validation_error: If any error happened in your form, this function returns the error message.


TheProfile view

  • createProfileView incustomers/views.py:
fromdjango.contribimportmessages# import messages to show flash message in your pagefromcustomers.formsimportProfileForm,form_validation_error# import the used form and related function to show errorsfromcustomers.modelsimportProfile# import the Profile Model@method_decorator(login_required(login_url='login'),name='dispatch')classProfileView(View):profile=Nonedefdispatch(self,request,*args,**kwargs):self.profile,__=Profile.objects.get_or_create(user=request.user)returnsuper(ProfileView,self).dispatch(request,*args,**kwargs)defget(self,request):context= {'profile':self.profile}returnrender(request,'customers/profile.html',context)defpost(self,request):form=ProfileForm(request.POST,request.FILES,instance=self.profile)ifform.is_valid():profile=form.save()# to save user model infoprofile.user.first_name=form.cleaned_data.get('first_name')profile.user.last_name=form.cleaned_data.get('last_name')profile.user.email=form.cleaned_data.get('email')profile.user.save()messages.success(request,'Profile saved successfully')else:messages.error(request,form_validation_error(form))returnredirect('profile')

dispatch method: In this section, if the user does not have a profile, it will be created and the value of the profile object will set in the profile field.


TheProfile HTML template

The template that handles the user input is defined incustomers/profile.html file.

<formaction="{% url 'profile' %}"method="POST"enctype="multipart/form-data">        {% csrf_token %}<div><labelfor="first_name">First Name</label><inputname="first_name"class="form-control"id="first_name"type="text"placeholder="Enter your first name"value="{{ profile.user.first_name }}"required></div><div><labelfor="last_name">Last Name</label><inputname="last_name"class="form-control"id="last_name"type="text"placeholder="Also your last name"value="{{ profile.user.last_name }}"required></div><labelfor="gender">Gender</label><selectname="gender"class="form-select mb-0"id="gender"aria-label="Gender select example"><optionselected>Gender</option>            {% for key, value in profile.GENDER_CHOICES %}<optionvalue="{{ key }}"{%ifprofile.gender ==key%}selected{%endif%}>{{ value }}</option>            {% endfor %}</select><!-- And the rest of the fields --></form>

Note: Make sure there isenctype="multipart/form-data" attribute in the form tag to upload the avatar.


TheProfile routing

Activate the routing forcustomers app by edit thecustomers/urls.py file with the following code:

fromdjango.urlsimportpathfromcustomersimportviewsurlpatterns= [path('profile/',views.ProfileView.as_view(),name='profile'),]

Updatecore/urls to includecustomers urls:

fromdjango.confimportsettingsfromdjango.contribimportadminfromdjango.conf.urls.staticimportstaticfromdjango.urlsimportpath,includeurlpatterns= [path('admin/',admin.site.urls),# Django admin routepath('customers/',include("customers.urls")),# Django customers route# ...]# to support and show media & static files in developer modeifsettings.DEVEL:urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)

Links & Resources



Django Template Volt - Provided by Themesberg and AppSeed.


[8]ページ先頭

©2009-2025 Movatter.jp