程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Django personal homepage 3 Application development of personal data back-end management

編輯:Python

Project full text index

『Python-Django Personal home page 』 Open source project overview

List of articles

  • Effect display
  • Application creation & To configure
  • Model models.py
  • management adminx.py
  • View Views.py
  • Application routing urls.py
  • Project routing urls.py
  • Migration command

Effect display

essential information
Education and career

Service data



We-Media

Guest message

Application creation & To configure

Application creation

CMD Command line to create navigation data application , And move the created application folder to apps Then you can .

cd MyHome
python manage.py startapp MyHomePage

Application configuration

stay settings.py Add an application name to INSTALLED_APPS .

INSTALLED_APPS = [
...
'apps.MyHomePage',

Other documents

  • admin.py Due to the use xadmin Replace the original version of admin, So it was renamed adminx.py.
  • test.py Test execution script , Deleting .
  • urls.py Create a new file to apply routing configuration .

Model models.py

Develop data model application according to project requirements .

# coding:utf-8
__author__ = 'Mr. Data Yang '
__explain__ = ' My home page model file '
from django.db import models
from django.utils import timezone
# My basic information 
class MyInformation(models.Model):
my_info = models.CharField(
max_length=255,
verbose_name=" My basic information ", help_text=" My basic information "
)
full_name = models.CharField(
max_length=10,
verbose_name=" My name ", help_text=" My name "
)
age = models.CharField(
max_length=2,
verbose_name=" My age ", help_text=" My age "
)
address = models.CharField(
max_length=20,
verbose_name=" My address ", help_text=" My address "
)
email = models.EmailField(
max_length=30,
verbose_name=" My mailbox ", help_text=" My mailbox "
)
phone = models.CharField(
max_length=15,
verbose_name=" contact number ", help_text=" contact number "
)
wechart = models.CharField(
max_length=20,
verbose_name=" My WeChat ", help_text=" My WeChat "
)
job = models.CharField(
max_length=20,
verbose_name=" My profession ", help_text=" My profession "
)
qq = models.CharField(
max_length=20,
verbose_name=" my QQ", help_text=" my QQ"
)
# My skills 
skill_info = models.CharField(
max_length=255,
verbose_name=" Basic skill information ", help_text=" Basic skill information "
)
# My education & Career Portfolio 
education_experience_info = models.CharField(
max_length=255,
verbose_name=" education & Career Portfolio ", help_text=" education & Career Portfolio "
)
# My paid service 
service_info = models.CharField(
max_length=255,
verbose_name=" Introduction to charging services ", help_text=" Introduction to charging services "
)
# My we media 
site_info = models.CharField(
max_length=255,
verbose_name=" We media introduction ", help_text=" We media introduction "
)
class Meta:
verbose_name = ' My basic information '
verbose_name_plural = verbose_name
def __str__(self):
return self.full_name
# My educational information 
class EducationExperienceList(models.Model):
EducationExperienceChoices = (
("0", " Educational information "),
("1", " Career Portfolio ")
)
education_experience_type = models.CharField(
choices=EducationExperienceChoices,
max_length=1,
verbose_name=" Category selection ", help_text=" Data category selection "
)
name = models.CharField(
max_length=30,
verbose_name=" Name of school or enterprise ", help_text=" Name of school or enterprise "
)
duration_time = models.CharField(
max_length=13, default="yyyy - yyyy",
verbose_name=" Time information ", help_text=" Time information "
)
info = models.CharField(
max_length=50,
verbose_name=" Introduction to schools or enterprises ", help_text=" Introduction to schools or enterprises "
)
class Meta:
verbose_name = ' My education & Career resume list '
verbose_name_plural = verbose_name
def __str__(self):
return self.name
# My service information 
class MyServiceProjectType(models.Model):
type_en = models.CharField(
max_length=20, default='',
verbose_name=" English category name ", help_text=" English category name "
)
type_cn = models.CharField(
max_length=20,
verbose_name=" Chinese category name ", help_text=" Chinese category name "
)
class Meta:
verbose_name = ' Service category information '
verbose_name_plural = verbose_name
def __str__(self):
return self.type_cn
class MyServiceProjectList(models.Model):
type_name = models.ForeignKey(
MyServiceProjectType, on_delete=models.CASCADE,
verbose_name=' Service category ', help_text=" Service category "
)
title = models.CharField(
max_length=30,
verbose_name=" The service name ", help_text=" The service name "
)
info = models.CharField(
max_length=50,
verbose_name=" Service profile ", help_text=" Service profile "
)
img = models.ImageField(
upload_to='MyServiceProject/', default='MyServiceProject/default.png',
verbose_name=' Service cover ', help_text=" If no cover is selected , The default cover page is used ",
null=True, blank=True
)
url = models.URLField(
max_length=500,
verbose_name=" Outside the service chain ", help_text=" Outside the service chain "
)
class Meta:
verbose_name = ' My service details '
verbose_name_plural = verbose_name
def __str__(self):
return self.title
# My blog homepage 
class MyBlogList(models.Model):
title = models.CharField(
max_length=30,
verbose_name=" We media site name ", help_text=" We media site name "
)
info = models.CharField(
max_length=50,
verbose_name=" We media profile ", help_text=" We media profile "
)
img = models.ImageField(
upload_to='MyBlog/', default='MyBlog/default.png',
verbose_name=' We media cover ', help_text=" If no cover is selected , The default cover page is used ",
null=True, blank=True
)
url = models.URLField(
max_length=500,
verbose_name=" Outside the service chain ", help_text=" Outside the service chain "
)
class Meta:
verbose_name = ' We media list '
verbose_name_plural = verbose_name
def __str__(self):
return self.title
# Contact my information 
class ContactMe(models.Model):
name = models.CharField(
max_length=50,
verbose_name=" full name ", help_text=" full name "
)
email = models.CharField(
max_length=50,
verbose_name=" mailbox ", help_text=" mailbox "
)
subject = models.CharField(
max_length=50,
verbose_name=" The theme ", help_text=" The theme "
)
message = models.TextField(
default="",
verbose_name=" Content ", help_text=" Content "
)
add_time = models.DateTimeField(
default=timezone.now,
verbose_name=" Submission time ", help_text=" Submission time "
)
class Meta:
verbose_name = ' Contact my information '
verbose_name_plural = verbose_name
def __str__(self):
return self.subject

management adminx.py

Develop data model application management according to project requirements .

# -*- coding: utf-8 -*-
__author__ = 'Mr Data Yang '
__explain__ = ' My home page adminx Background configuration file '
from .models import *
from xadmin import views
import xadmin
# My basic information 
class MyInformationAdmin(object):
list_display = ['full_name', 'age', 'email']
show_bookmarks = False
class EducationExperienceListAdmin(object):
list_display = ['education_experience_type', 'name', 'duration_time', 'info', ]
show_bookmarks = False
class MyServiceProjectTypeAdmin(object):
list_display = ['type_en', 'type_cn']
show_bookmarks = False
class MyServiceProjectListAdmin(object):
list_display = ['type_name', 'title', 'info', 'url']
show_bookmarks = False
class MyBlogListAdmin(object):
list_display = ['title', 'info', 'url', ]
show_bookmarks = False
# Contact my information 
class ContactMeAdmin(object):
list_display = ['subject', 'email', 'add_time', ]
show_bookmarks = False
xadmin.site.register(MyInformation, MyInformationAdmin)
xadmin.site.register(EducationExperienceList, EducationExperienceListAdmin)
xadmin.site.register(MyServiceProjectType, MyServiceProjectTypeAdmin)
xadmin.site.register(MyServiceProjectList, MyServiceProjectListAdmin)
xadmin.site.register(MyBlogList, MyBlogListAdmin)
xadmin.site.register(ContactMe, ContactMeAdmin)

View Views.py

Develop data model view management according to project requirements .

# coding:utf-8
from django.shortcuts import render
from django.views.generic.base import View # Class based implementation requires inheritance view
from apps.MyHomePage.models import *
from django.http import HttpResponse
class MyHomeIndex(View):
# My basic information 
MyInformation = MyInformation.objects.all()
# My education & Career Portfolio 
EducationExperienceList = EducationExperienceList.objects.all()
# My services 
MyServiceProjectType = MyServiceProjectType.objects.all()
MyServiceProjectList = MyServiceProjectList.objects.all()
# My we media 
MyBlogList = MyBlogList.objects.all()
# Contact me 
ContactMeData = ContactMe()
def get(self, request):
return render(request, 'HomePage/index.html', {

'MyInformation': self.MyInformation[0],
'MySkill': self.MyInformation[0].skill_info,
'EducationExperience': self.MyInformation[0].education_experience_info,
'EducationExperienceList': self.EducationExperienceList,
'MyServiceProject': self.MyInformation[0].service_info,
'MyServiceProjectType': self.MyServiceProjectType,
'MyServiceProjectList': self.MyServiceProjectList,
'MyBlog': self.MyInformation[0].site_info,
'MyBlogList': self.MyBlogList,
})
def post(self, request):
name = request.POST.get('name')
email = request.POST.get('email')
title = request.POST.get('title')
message = request.POST.get('message')
ContactMeData = self.ContactMeData
ContactMeData.name = name
ContactMeData.email = email
ContactMeData.title = title
ContactMeData.message = message
ContactMeData.save()
return HttpResponse(" Thank you for your submission ")

Application routing urls.py

# coding:utf-8
__author__ = 'Mr. Data Yang '
__explain__ = ''
from django.urls import path
from .views import *
app_name = 'MyHomePage'
urlpatterns = [
path('', MyHomeIndex.as_view(), name='MyHomeIndex'),
]

Project routing urls.py

urlpatterns = [
......
path('', include('MyHomePage.urls', namespace='MyHomePage')),
]

Migration command

Carry out the routine CMD Command to migrate the project .

python manage.py makemigrations # Migrate to our database 
python manage.py migrate # Synchronize to database 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved