


Implementation method of Django book character adaptation system in python (front-end)
The content of this article is about the implementation method (front-end) of the Django book character adaptation system in python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Django adds routing
Like flask, django also needs to use routing to associate the URL with the code to be executed on the server side.
The same thing about both is that they can turn an ordinary function into a view function. The difference is that flask uses the decorator @app.route() to define routes, while django uses regular expressions to define routes.
Operation: Create a new urls.py file in the book project we created
#FristDjango\book\urls.py from django.conf.urls import url from django.contrib import admin from book.views import index,detail urlpatterns = [ url(r'^$',index), #http://127.0.0.1:9099/ url(r'book/(?P<id>\d+)/$',detail) #http://127.0.0.1:9099/book/1/ ]</id>
Modify the setting.py file of the main project
from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'',include('book.urls')) ]
View function You can write it as you like, now it is only used for testing
#FristDjango\book\views.py from django.http import HttpResponse from django.shortcuts import render # Create your views here. def index(request): #django不同于flask,flask默认请求方式是request,而django里面的请求对象必须作为参数接收 return HttpResponse('ok') def detail(request,id): return HttpResponse('%s is ok' %(id))
^ matches the beginning of the URL path, $ matches the end of the URL path. There is nothing in the middle, indicating that this regular match is the root directory, '/'.
Simple web page structure
Have tested that the web page is available, now write your own html file to implement your own project
Our web template files are placed in the application files in the templates/ directory of the main project. There may be multiple applications, so different directories are created to distinguish them.
It is necessary to achieve integration with the database. The database information managed in the background should be displayed in the foreground and the view function should be rewritten
#FristDjango\book\views.py from django.http import HttpResponse from django.shortcuts import render # Create your views here. from book.models import BookInfo,HeroInfo def index(request): #django不同于flask,flask默认请求方式是request,而django里面的请求对象必须作为参数接收 # return HttpResponse('ok') books = BookInfo.objects.all() return render(request,'book/index_old.html',context={ 'books':books, }) def detail(request,id): # return HttpResponse('%s is ok' %(id)) book = BookInfo.objects.get(id=id) heros = book.heroinfo_set.all() return render(request,'book/detail_old.html',context={ 'book':book, 'heros':heros })
The html page displayed on the homepage
# templates/book/index_old.html nbsp;html> <meta> <title>主页</title>
-
{% for book in books %}
-
{{ book.btitle }}
{{ book.bpub_time }}
{{ book.bcontent }}
{% endfor %}
The html page displayed on the book details page
# templates/book/detail_old.html nbsp;html> <meta> <title>{{ book.btitle}}详情页</title> <h1 id="book-btitle">{{ book.btitle }}</h1>
-
{% for hero in heros %}
{{ hero.hname }}
{{ hero.hcontent }}
{% endfor %}How to make the page look better
step1: There is too little book information, modify the book database table structure, add book summary, Book pictures
step2: Find some good-looking page files from the Internet to make modifications
注意:静态文件(CSS,JS,IMG)单独存放一个目录static/ 样式文件的目录需要修改,主项目settings.py文件修改Static files
step1 Operation: Modify the structure of the BookInfo table in the book/models.py file and add abstracts and pictures. Create a new static/uploads/ directory to store uploaded images.
bcontent = models.TextField(default='摘要', verbose_name='书籍摘要') bimg = models.ImageField(default=None, upload_to='static/uploads/')
Terminal execution database migration command
python manage.py makemigrations python manage.py migrate
Use the administrator to log in to the background and modify the book content
step2 operation: After creating the static/directory, modify the main project settings.py file
# Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.10/howto/static-files/ STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), ]
Note: There is a lot of repeated code on the homepage and details page. We create a base template and let the application template inherit from the base class to reduce the amount of code duplication.
The last modified web page is shown in the figure:
Project framework diagram:
The above is the detailed content of Implementation method of Django book character adaptation system in python (front-end). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

Use the EXPLAIN command to analyze the execution plan of MySQL queries. 1. The EXPLAIN command displays the execution plan of the query to help find performance bottlenecks. 2. The execution plan includes fields such as id, select_type, table, type, possible_keys, key, key_len, ref, rows and Extra. 3. According to the execution plan, you can optimize queries by adding indexes, avoiding full table scans, optimizing JOIN operations, and using overlay indexes.
