1024programmer Java Django other (site, list, upload

Django other (site, list, upload

1.Static files:

  • The CSS, images, and js in the project are all static files
  • Static files are generally placed in a separate directory to facilitate management
  • When calling in an html page, you also need to specify the path of the static file. Django provides a parsing method to configure the static file path
  • Static files can be placed in the project root directory or in the application directory. Since some static files are common in projects, it is recommended to place them in the project root directory for easy management

1.Define the static file search path in the project/settings.py file

STATIC_URL =’/static/’
STATICFILES_DIRS = [
os.path.join(BASE_DIR,’static’),
]

2.Create a static directory in the project root directory, and then create img and css , js directory

3.Define the view jingtai in the application name booktest/views.py

defjingtai(request):
returnrender(request,’booktest/jingtai.html’)

4.Configure url in booktest/urls.py

url(r’^jingtai/$’,views.jingtai),

5.Create jingtai.html file under templates/booktest/

Dynamic configuration:
{%load static from staticfiles%}

1.Question 1:djangoHow to determine the current request for a static file?

http://127.0.0.1:8000/static/images/bjt.png

Answer: Request path/static/images/ bjt.pngThe beginning of /static/

withsettings.pySTATIC_URLCompare, if they are the same, they are considered static files

2.Question 2: In which directory on the disk are static files found?

Answer: The remaining path after judgmentimages/ bjt.png

Option 1: with settings.pySTATICFILES_DIRSSplicing

Option 2: In the application directorystatic Search in directory

2.Middleware

  • Middleware in Django is a lightweight, low-level plug-in system that can intervene in Django’s request and response processing and modify Django’s input or output
  • The design of middleware provides developers with a non-intrusive development method and enhances the robustness of the Django framework. Other MVC frameworks also have this function, named IoC
  • Django executes common methods in middleware at different stages:
  • process_request
  • process_view
  • process_response

1.Question 1: When to use middleware?

Answer: When the same piece of code needs to be executed in most views

Object

  • You cannot directly access the properties or methods of the associated object. You can encapsulate the methods in the model class to access the members of the associated object
  • Open the booktest/models.py file and modify the AreaInfo class as follows

classAreaInfo(models.Model):

def parent(self):
return self.aParent.atitle
parent.short_description=’Parent region name

Right column filter

  • The attributes are as follows, only fields can be received, and the values ​​of the corresponding fields will be listed for quick filtering

list_filter=[]

Search box

  • The following attributes are used to search for the value of the specified field and support fuzzy query

search_fields=[]

Chinese title

  • Open the booktest/models.py file, modify the model class, and specify the verbose_name parameter for the attribute, which is the first parameter

classAreaInfo(models.Model):
atitle=models.CharField(‘
Title‘,max_length=30)#Name

Edit page options

Show field order

  • The properties are as follows

fields=[]

Group display

  • The properties are as follows

fieldset=(
(‘
Group1Title‘,{‘fields’:(‘Fields 1′,’Field2′) }),
(‘
Group2Title‘,{‘fields’:(‘Fields3′,’Field4′)}),
)

Associated objects

  • In a one-to-many relationship, multi-end objects can be edited on the editing page of one end. The methods of embedding multi-end objects include tables and blocks
  • Type InlineModelAdmin: Indicates embedding the editing of associated models in the model’s editing page
  • Subclass TabularInline: Embed in table form
  • Subclass StackedInline: embedded in blocks
  • Open the booktest/admin.py file and create the AreaStackedInline class

classAreaStackedInline(admin.StackedInline):
model = AreaInfo#
Associated sub-objects
extra = 2#
Extra edit2 Sub-object

Rewrite template

  • Create the admin directory under the templates/ directory
  • Open the Django directory in the current virtual environment, and then find the admin template
  • Copy the files that need to be changed to the directory created in the first step, and edit the files

4.Upload pictures

  • There are two ways to upload images in Django:
  • 1.Upload pictures in the management page admin
  • 2.Upload images in custom form
  • After uploading the image, store the image on the server, and then store the path of the image in the table

Upload pictures in the management page admin

Register:admin.site.register(PicTest)

Upload pictures in custom forms

1.Open the booktest/views.py file and create a view

  • 2.Open the booktest/urls.py file and configure url
  • 3.Create template pic_upload.html in the templates/booktest/ directory
  • Note: Define the upload form in the template, the requirements are as follows
  • Form attributesenctype=” multipart/form-data”
  • form’s method is post
  • The input type is file

5.Paging

  • Django provides classes for data pagination, which are defined in django/core/paginator.py
  • The object Paginator is used to perform pagination operations on columns with n pieces of data on one page
  • The object Page is used to represent the data on page m

Paginator object

  • Method init (list, int): Returns the paging object, the parameters are list data, the number of data on each side
  • Attribute count: Returns the total number of objects
  • Attributesnum_pages: Returns the total number of pages
  • Attributespage_range: Returns a list of page numbers, starting from 1, such as [1, 2, 3, 4]
  • Methodpage(m): Returns the Page object, representing the data on page m, and the subscript starts with 1

Page object

  • Callpage() method of Paginator objectReturns Page object, no manual construction is required
  • Attribute object_list: Returns the list of objects on the current page
  • Attributesnumber: Returns the current page, starting from 1
  • Attribute paginator: Paginator object corresponding to the current page
  • Method has_next(): Returns True if there is a next page
  • Method has_previous(): Returns True if there is a previous page
  • Method len(): Returns the number of objects on the current page
  • Iterate page objects: access each object in the current page

File, configuration url

  • 3.Create template pic_upload.html in the templates/booktest/ directory
  • Note: Define the upload form in the template, the requirements are as follows
  • Form attributesenctype=” multipart/form-data”
  • form’s method is post
  • The input type is file
  • 5.Paging

    • Django provides classes for data pagination, which are defined in django/core/paginator.py
    • The object Paginator is used to perform pagination operations on columns with n pieces of data on one page
    • The object Page is used to represent the data on page m

    Paginator object

    • Method init (list, int): Returns the paging object, the parameters are list data, the number of data on each side
    • Attribute count: Returns the total number of objects
    • Attributesnum_pages: Returns the total number of pages
    • Attributespage_range: Returns a list of page numbers, starting from 1, such as [1, 2, 3, 4]
    • Methodpage(m): Returns the Page object, representing the data on page m, and the subscript starts with 1

    Page object

    • Callpage() method of Paginator objectReturns Page object, no manual construction is required
    • Attribute object_list: Returns the list of objects on the current page
    • Attributesnumber: Returns the current page, starting from 1
    • Attribute paginator: Paginator object corresponding to the current page
    • Method has_next(): Returns True if there is a next page
    • Method has_previous(): Returns True if there is a previous page
    • Method len(): Returns the number of objects on the current page
    • Iterate page objects: access each object in the current page

    This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/django-other-site-list-upload/

    author: admin

    Previous article
    Next article

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Contact Us

    Contact us

    181-3619-1160

    Online consultation: QQ交谈

    E-mail: [email protected]

    Working hours: Monday to Friday, 9:00-17:30, holidays off

    Follow wechat
    Scan wechat and follow us

    Scan wechat and follow us

    Follow Weibo
    Back to top
    首页
    微信
    电话
    搜索