In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you ubuntu16.04 in python3 to create a Django project and run the example analysis, I hope you will learn something after reading this article, let's discuss it!
Step 1: create a django project
Open the terminal and switch to the address of the desired project: cd python3_django_projects
Enter the command: django-admin.py startproject Hello; (that is, a project named Hello has been created)
Step 2: start the project
Enter the Hello directory: cd Hello
Enter the command python manage.py runserver; or python3 manage.py runserver; (corresponding to the python version yourself)
Step 3: enter http://127.0.0.1:8000/ or: http://localhost:8000 in the browser (8000 is the default port number)
If you need to modify the port number: python manage.py runserver 9999; or python3 manage.py runserver 9999
Enter: http://127.0.0.1:9999/ or: http://localhost:9999 in the browser
When you see this page, you are successful.
Step 4: use pytharm (not necessarily pycharm, your accustomed python compiler will do) to open the Hello project you created, and remember to change the corresponding interpreter. Ubuntu16.04 comes with python2.7 and python3.5, and I downloaded python3.6, and django is also in python3.6, so you need to change the interpreter in pycharm.
Step 5: project catalog introduction:
1.manage.py: the entry point to the command line toolset that interacts with the project, the project manager. You can execute python manage.py or python3 manage.py to view all commands. (like the runserver above is to start the server)
2.Hello directory: a container of the project, which contains some of the most basic configurations of the project. The directory name can be modified, but it is not recommended, because many configuration files have written many configurations with this name, which affects the whole body. If you really can't stand loneliness or like in-depth understanding, you can try to change it. As a novice, I didn't try anyway.
Wsgi.py: Chinese name: python server network management interface. The interface between the python application and the web server is very important, don't touch it easily, it will be troublesome if you break it, and you won't use it when writing a project. Let him be a beautiful man quietly.
Urls.py:URL configuration file, all addresses (pages) in the Django project need to be configured by ourselves.
URLsettings.py: a very important file, which is the core file of the whole project and the total configuration file of the project. It contains database, Web application, time and other configurations.
1.BASE_DIR: the root directory of the project
two。 The project security code, which is required to start the project, is automatically generated by Django when it is created.
3. DEBUG
There must be bug in the code. If DEBUG = True, the exception will be passed directly to the front end of the web page. It is usually opened when writing the code, which is easy to find errors, but it must be turned off in actual production and cannot be seen by the user. ALLOWED_HOSTS = []: if you add something to it, such as ALLOWED_HOSTS = [localhost], Django is only allowed to access the page through localhost, everything else is blocked, 127.0.0.1 will not work.
4.INSTALLED_APPS = []
There are some applications that come with Django. After we create the application, we have to write the name in it before it can be recognized by Django, otherwise it won't recognize it.
5.MIDDLEWARE = []: it is called middleware in Chinese translation.
Some of the toolsets that come with Django, you don't need to worry about it, just know.
6.ROOT_URLCONF = 'Hello.urls'
The root file of URL points to the urls.py file mentioned above.
7.TEMPLATES = [] template
In Django, a template is a html file, and the templates here is the configuration of the template. Don't worry about it for the time being. Come back later when you write your own template.
8.WSGI_APPLICATION, regardless of it, as a novice, I don't understand either.
DATABASES
Database related configuration
Sqlite3 database is used by default. If you want to use another database, go to the link address annotated above to find out how to configure it. You don't have to change it without other databases.
10.AUTH_PASSWORD_VALIDATORS = [], password authentication is related, don't worry about it now.
11.
Internationalization, that is, time, language and so on, LANGUAGE_DODE = 'en-us', defaults to English, and TIME_ZONE =' UTC' defaults to the UTC time zone.
twelve。 Static file address
Addresses of documents such as css,javascript,images
The file that declares the module in 4.__init__.py:python is generally empty. With it, our myblog becomes a module that can be referenced directly in the code.
Now that you have learned all the files in this directory, let's create an application.
Step 6: create an application.
Change to the same directory as manage.py and type python3 manage.py startapp hello_world or python3 manage.py startapp hello_world on the command line to create an application called hello_world. The file structure is:
Then remember to add the application name to the INSTALLED_APPS of settings.py
An application is created successfully!
Step 7: understand the role of files under the application:
1 Django migrations: data migration module, database-related operations, everything under this folder is automatically generated by the database, you can ignore it.
2 Django admin.py: the background management system configuration file under this application. Each application has its own background system.
3Luminapps.py: some configurations of the current application will not be automatically generated until after Django 1.9. there are no previous versions, so in theory, we don't have to use it.
4 Django models.py: data module, later create data tables are created here, Django involves a framework called Baidu, want to know can be their own Baidu, I do not know what it is. Similar to Models (model) in MVC structure.
5Jing test.py: automated test module, we can write the corresponding code (script) here, and then we can test accordingly.
6Jing views.py: the logical code that executes the response, and the main place where the code logic is processed. Most of the code for the project is written here.
Step 8: create the first page (response)
1. Edit the views.py under hello_wrold:
2. Configure URLS:
Open urls.py under the Hello directory
After saving the file, type http://127.0.0.1:8000/index/ or http://localhost:8000/index/ in the browser to see the hello,world.
Urlpatterns = [] is a list, which contains the paths of all the web pages in this project, the web address of hello,world that we just wrote when index/, and a function under hello when hello.index, but we imported the views.py file under the hello_world application as a function under the hello, so hello.index is a function under the views.py file under the hello_world application.
After reading this article, I believe you have some understanding of "sample Analysis of Django Project created and run by ubuntu16.04 under python3". If you want to know more about it, please follow the industry information channel. Thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.