Python manage.py startproject MySite
Python mange.py Startapp uploads
Setting Configuration
Installed_apps Add the app name ' uploads ',
Static_url = '/static/'
Media_url = '/media/'
Media_root = Os.path.join (base_dir, ' media ')
Root URL Configuration
From Django.conf.urls import include, Url,re_pathfrom django.contrib import adminfrom django.conf.urls.static Import Staticfrom django.conf Import settingsurlpatterns = [ url (R ' ^admin/', admin.site.urls), re_path (' uploads/', Include (' Uploads.urls ')] + static (settings. Media_url,document_root=settings. Media_root)
MODEL Configuration
From django.db import Modelsclass User (models. Model): gender_choices = (' 0 ', ' Male '), (' 1 ', ' female '), (' 2 ', ' shemale ') ') name = models. Charfield (' username ', max_length=256) sex = models. Charfield (max_length=16, choices=gender_choices, default= ' 0 ') img = models. ImageField (upload_to= ' images/', verbose_name= ' user avatar ') introduce = models. Filefield (verbose_name= ' user cv ', upload_to= ' introduce/') def __str__ (self): return Self.name class Meta: verbose_name = "user" verbose_name_plural = "User"
< Span style= "COLOR: #f92672" > < Span style= "COLOR: #f92672" >
Execute python manage.py makemigrations,migrate create database
admin configuration
From Django.contrib import Adminfrom. Import Modelsadmin.site.register (models. User)
URL Configuration
From Django.urls import Pathfrom. Import viewsurlpatterns = [ path (' add/', views.add), path (' detail/', Views.detail),]
View Configuration
From django.shortcuts import renderfrom. Models import userdef Add (Request): if Request.method = = ' POST ': Username = Request. Post.get (' username ') img = Request. Files.get (' img ') introduce = Request. Files.get (' introduce ') user = User (name=username, img=img, Introduce=introduce) user.save () return Render (Request, ' uploads/add.html ', Locals ()) return render (Request, ' uploads/add.html ', Locals ()) def detail ( Request): user_list = User.objects.all () return render (Request, ' uploads/detail.html ', locals ())
create templates/uploads/add.html & detail.html file
add.html
Detail.html
{% for user in user_list%}
done
< Span style= "COLOR: #e6db74" > < Span style= "COLOR: #e6db74" >&NBSP;
Django---uploads files