The editor uses the Django framework in Python to complete it! 1. First, use pycharm to create a Django project and configure the relevant environmentHere the editor will create a default project Two configurations to be modified in settings.py DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 'ENGINE': 'django.db.backends.mysql', 'NAME': 'photos', 'HOST': '127.0.0.1', 'PORT': '3306', 'USER': 'root', 'PASSWORD': '201314', } } STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ] 2. Create a table① First press the win+s key on the keyboard, then enter cmd, press Enter twice for Chinese input method, and press Enter once for English input method to enter the DOS window. ② Enter mysql -uroot -p password and press Enter to enter the mysql database, then enter create database table name; a small Enter to create the database🆗 ③Create the table structure in models.py under app models.py from django.db import models # Create your models here. class Images(models.Model): img = models.ImageField(upload_to='static/pictures/') # upload_to='static/pictures/' specifies the folder name for image storage. It will be automatically created after uploading the file. img_name = models.CharField(max_length=32) create_time = models.DateTimeField(auto_now_add=True) ④Migrate the database Execute the following two statements in the Terminal under pycharm in order python manage.py makemigrations python manage.py migrate 3. Upload picture functionurls.py from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/$', admin.site.urls), url(r'^upload/$', views.upload, name='upload'), ] views.py from django.shortcuts import render, redirect from app01 import models # Create your views here. def upload(request): error = '' if request.method == 'POST': img = request.FILES.get('img') pic_name = img.name if pic_name.split('.')[-1] == 'mp4': error = 'Uploading images of this format is not supported yet! ! ! ' else: models.Images.objects.create(img_name=pic_name, img=img) return redirect('show') return render(request, 'upload.html', locals()) Front-end upload page upload.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload photos</title> </head> <body> <div style="height: 160px"> <form action="" method="post" enctype="multipart/form-data"> {% csrf_token %} <h1>Upload image page</h1> <table cellpadding="5px"> <tr> <td>Upload pictures</td> <td><input type="file" name="img"></td> </tr> <tr> <td> <button>Upload</button> </td> <td><strong style="color: red">{{ error }}</strong></td> </tr> </table> </form> </div> <div style="text-align: center;color: #2b542c;font-size: 20px;"> <a href="{% url 'show' %}" rel="external nofollow" >Back</a> </div> </body> </html> 4. Display picture functionurls.py """ from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/$', admin.site.urls), url(r'^upload/$', views.upload, name='upload'), url(r'^show/$', views.show, name='show'), ] views.py from django.shortcuts import render, redirect from app01 import models # Create your views here. def upload(request): error = '' if request.method == 'POST': img = request.FILES.get('img') pic_name = img.name if pic_name.split('.')[-1] == 'mp4': error = 'Uploading images of this format is not supported yet! ! ! ' else: models.Images.objects.create(img_name=pic_name, img=img) return redirect('show') return render(request, 'upload.html', locals()) def show(request): all_images = models.Images.objects.all() # for i in all_images: # print(i.img) return render(request, 'show.html', locals()) Front-end display show.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Show photos</title> </head> <body> {% for image in all_images %} <img src="/{{ image.img }}" style="width: 240px;height: 240px;"> {% endfor %} <br/> <p style="text-align: center;color: #2b542c;font-size: 20px;"> <a href="{% url 'upload' %}" rel="external nofollow" rel="external nofollow" >Back</a> </p> </body> </html> 5. Delete picture functionurls.py from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/$', admin.site.urls), url(r'^upload/$', views.upload, name='upload'), url(r'^show/$', views.show, name='show'), url(r'^delete/$', views.delete, name='delete'), ] views.py from django.shortcuts import render, redirect from app01 import models # Create your views here. def upload(request): error = '' if request.method == 'POST': img = request.FILES.get('img') pic_name = img.name if pic_name.split('.')[-1] == 'mp4': error = 'Uploading images of this format is not supported yet! ! ! ' else: models.Images.objects.create(img_name=pic_name, img=img) return redirect('show') return render(request, 'upload.html', locals()) def show(request): all_images = models.Images.objects.all() # for i in all_images: # print(i.img) return render(request, 'show.html', locals()) def delete(request): pk = request.GET.get('pk') models.Images.objects.filter(id=pk).delete() return redirect('show') show.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Show photos</title> </head> <body> {% for image in all_images %} <img src="/{{ image.img }}" style="width: 240px;height: 240px;"> <a href="/delete/?pk={{ image.id }}" rel="external nofollow" >Delete</a> {% endfor %} <br/> <p style="text-align: center;color: #2b542c;font-size: 20px;"> <a href="{% url 'upload' %}" rel="external nofollow" rel="external nofollow" >Back</a> </p> </body> </html> 6. Demonstrate the whole thing Because of the tight time, it is implemented in the lowest possible way, without adding beautiful pages and styles. Friends who like beauty can go to the Bootstrap official website or jq22 to add it by themselves! ! ! This is the end of this article about saving pictures to MySQL database and displaying them on the front-end page. For more relevant pictures saved in MySQL database and displayed on the front-end page content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Vue multi-page configuration details
>>: Seven Principles of a Skilled Designer (1): Font Design
Preface The reason for writing this article is mai...
By default, Flash will always be displayed at the ...
The machines in our LAN can access the external n...
If you don’t understand what I wrote, there may b...
A list is defined as a form of text or chart that...
The specific code of the sliding button made with...
Web page design related questions, see if you can...
Table of contents 1. Project requirements 2. Docu...
This article records the detailed tutorial for in...
In Linux, everything is a file (directories are a...
Table of contents 1. Introduction II. Monitoring ...
!DOCTYPE Specifies the Document Type Definition (...
A few days ago, I saw a post shared by Yu Bo on G...
If you need to use an image to implement the use ...
add -it docker run -it -name test -d nginx:latest...