Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create APP in Django

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to create an APP in Django. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Create APP in 1.Django

First, let's open PyCharm and find the Django project you created earlier. My project is First_Project:

(1) first select the project you want to run-> then click Tools- > Run manage.py Task- > to enter the manage.py console (or direct shortcut key Ctrl+Alt+R to enter)

(2) then enter the name startapp you want in the manage.py console to create the app. If I need to create an app named users, then I need to enter startapp users in the console

(3) you can create one or more app, and to facilitate the management of multiple app files, we can create an apps directory and move all the created app into it.

(4) if we want to directly use the app file import module in apps, we can set the apps directory to Sources Root by selecting the apps folder-> right-click-> Mark Directory as- > Sources Root, then pycharm will know that the root path of the file is apps

(5) after setting up, we can directly import modules in any app under the apps directory. But only run in PyCharm, if you want to run on the command line will report an error (because the Python interpreter believes that: the search path of the Python module is to start from the directory where the currently running Python file is located, and then in the root directory of the project, level by level, and finally to the site-packages directory).

So how to run without reporting errors on the command line, and apps is also Sources Root in command-line mode, we need to import the sys module in the project's settings.py file and add a line of code:

# the purpose is to add the apps path sys.path.insert (0, os.path.join (BASE_DIR, 'apps')) to the first location in the sys.path module search path of django

(6) finally, import the django and os modules in the file we need to execute, then add two sentences of code, and then import the modules in the app you want to use.

Os.environ.setdefault "DJANGO_SETTINGS_MODULE", "First_Project.settings") django.setup ()

(7) now you can execute it on the command line, such as python test.py.

Create a static static file directory in 2.Django

Configure the static static file lookup path:

1. First open the settings file and find STATIC_URL ='/ static/', which means that you specify the static static file path

two。 But this path cannot be used in template, because this path is not added to the project path, so we need to add the static path to the project path. We need to add a line of code:

# adds the static path to the project path STATICFILES_DIRS = (os.path.join (BASE_DIR, "static"),)

3. So you can reference the static file in template by simply specifying the relative path.

Create a media media file directory in 3.Django

Configure the media media file lookup path:

1. Add a MEDIA_ROOT = os.path.join (BASE_DIR, 'media') statement to the settings file to save all the things uploaded by the user, such as pictures, to this / media/ directory

two。 If we want the picture to be uploaded to the / media/banner/%Y/%m/ directory (Y for year, m for month), we can write

Image = models.ImageField (max_length=100, upload_to='banner/%Y/%m', verbose_name=' user profile picture')

3. So in the front-end html page, we can use the pictures uploaded by users or other things that can be displayed on the html page, because the path of picture upload is saved in the database, {{MEDIA_URL}} is equivalent to / media/, {{request.user.image}} is equivalent to user/1.png, then {{MEDIA_URL}} {{request.user.image}} is equivalent to / media/user/1.png

4. So you have to write a routing logic in the urls.py file.

From django.views.static import serveurl (r'^ media/ (? P.*)', serve, {'document_root': MEDIA_ROOT})

We should note that every time an app is created, it needs to be registered in the settings. If it is not registered, the table will not be generated when the migration is executed, and the name of the app will be added to the INSTALLED_APPS.

INSTALLED_APPS = ['django.contrib.admin',' django.contrib.auth', 'django.contrib.contenttypes',' django.contrib.sessions', 'django.contrib.messages',' django.contrib.staticfiles', 'users',' courses', 'teachers',' schools',] Thank you for reading! This is the end of the article on "how to create APP in Django". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report