In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Special note: this article is written for Django 1.11 and Python 3 or later. If the Django version does not match, you can skip the article or update your Django to the latest version.
1. Django installation steps
1. Installation version:
Django1.11.1
Django download address: https://www.djangoproject.com/download/
2. Supported PYTHON version
Django1.11 no longer supports python2.7, so it must be on the python3 version
3. Install python3+
Python download address: https://www.python.org/downloads/
4. Install PYTHON3+
To install Python, you just need to download the python-x.x.x.msi file and click the "Next" button all the time.
You need to set the Python environment variable after installation. Right-click computer-> Properties-> Advanced-> Environment variables-> modify the system variable path, add the Python installation address, this example uses C:\ Python34, you need to install according to your actual situation.
If it is a linux system, please download the Python-3.x.x.tgz installation package and install it yourself.
5. Django installation
A, × × installation
Unpack the Django package and place it in the same root directory as the Python installation directory, go to the Django directory, execute python setup.py install, and then start the installation. Django will be installed into site-packages under Python's Lib.
C:\ Python34\ Lib\ site-packages
Then configure the environment variables and add these directories to the system environment variables: C:\ Python33\ Lib\ site-packages\ django;C:\ Python33\ Scripts. After adding, you can use the django-admin.py command of Django to create a new project.
Check whether the installation is successful
Python-m django-- version or
> import django
> > print django.get_version ()
If Django is already installed, you should see the installed version. If it is not installed, you will see a "No module named django" error.
B, pip commands are installed online
The first step is to install the pip command
Download the pip command, unzip it to the pip folder, install python setup.py install, and then you can use the pip command.
Pip install Django==1.11.1
6. On how to delete the old version of Django and install a new one
If you are upgrading the installation of Django from a previous version, you need to uninstall the old version of Django before installing the new version
If you have previously installed Django using pip or easy_install, then using pip or easy_install installation again will automatically deal with the old version, so you don't have to do it yourself.
If you previously installed Django using python setup.py, the uninstall operation is like removing the django directory from your Python site-packages.
Find
$python-c "import sys; sys.path = sys.path [1:]; import django; print (django.__path__)"
This completes the installation.
Second, create a project
1 、 Creating a project
Django-admin startproject mysite
Note: you will need to avoid naming the project after the built-in Python or Django components. In particular, this means that you should avoid using names like djangoDjango itself (or test conflicts with the built-in Python package)
2. Description of files in mysite project
Mysite/
Manage.py
Mysite/
_ _ init__.py
Settings.py
Urls.py
Wsgi.py
The external mysite/ root is just a container for a project. Its name has nothing to do with Django; you can rename it to anything you like.
Manage.py: a command-line utility that lets you interact with this Django project in a variety of ways. You can read all the details in django-admin and manage.pymanage.py.
The internal mysite/ directory is the actual Python package for your project. Its name is the name of the Python package (for example, mysite.urls) that you need to import anything in it.
Mysite/__init__.py: an empty file that tells Python that this directory should be considered a Python package. If you are a beginner in Python, please read more about software packages in the official Python documentation.
Mysite/settings.py: the setup / configuration of this Django project. The Django setting will tell you all about how the setting works.
Mysite/urls.py: the URL statement of the Django project; the "directory" of your Django Dynamics website. You can read more about URL in the URL scheduler.
An mysite/wsgi.py:WSGI-compliant Web server provides an entry point for your project to serve. For more information, see how to deploy using WSGI.
3. Development server
We are here to verify the work of your Django project. Change to an external mysite directory, if you don't already have one, and run the following command:
$python manage.py runserver
Startup information
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
May 20, 2017-15:50:53
Django version 1.11, using settings' mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
You have started using the Django development server, a lightweight Web server written purely in Python. We combine it with Django so that you can develop quickly without having to deal with configuring a production server such as Apache.
Now that the server is running, please use your Web browser to access http://127.0.0.1:8000/. You will see a "Welcome to Django" page, in a pleasant, light blue pastel color. It works!
Change Port
By default, this runserver command starts the development server on the internal IP on port 8000.
If you want to change the port of the server, pass it as a command line argument. For example, this command starts the server on port 8080:
$python manage.py runserver 8080
If you want to change the IP of the server, pass it along with the port. For example, to monitor all available public IP (if you are running Vagrant or want to show off your work on other computers on the network), use:
$python manage.py runserver 0RO 8000
0 is a shortcut to 0.0.0.0. The complete documentation for the development server can be found in the references for runserver.
Automatically reinstall runserver
The development server automatically reloads the Python code for each request as needed. You do not need to restart the server for the code changes to take effect. However, some actions, such as adding files, do not trigger a restart, so you must restart the server in these cases.
4 、 Creating the Polls app
Now that your environment-a "project"-has been established, you will begin to work.
Each application you write in Django contains Python packages that follow certain conventions. Django comes with a utility that automatically generates the basic directory structure of the application, so you can focus on writing code instead of creating directories
Your application can live anywhere in the Python path. In this tutorial, we will create our voting application next to your manage.py file so that it can be imported as its own top-level module rather than as a submodule mysite.
To create your application, make sure you are in the same directory as the directory, manage.py, and type the following command:
$python manage.py startapp polls
Polls/
_ _ init__.py
Admin.py
Apps.py
Migrations/
_ _ init__.py
Models.py
Tests.py
Views.py
5 、 Write your first view
Let's write the first opinion. Open the file polls/views.py and place the following Python code:
Polls/views.py
From django.http import HttpResponse
Def index (request):
Return HttpResponse ("Hello, world. You're at the polls index.")
This is the simplest view in Django. To invoke the view, we need to map it to a URL, and to do that, we need a URLconf.
To create a URLconf in the polls directory, create one named urls.py. Your application directory should look like this:
Polls/
_ _ init__.py
Admin.py
Apps.py
Migrations/
_ _ init__.py
Models.py
Tests.py
Urls.py
Views.py
Include the following code in the polls/urls.py file:
Polls/urls.py
From django.conf.urls import url
From. Import views
Urlpatterns = [
Url (r'^ $', views.index, name='index')
]
The next step is to point the root URLconf to the polls.urls module. Add a urlpatterns list of import for django.conf.urls.include and insert include () in mysite/urls.py:
Mysite/urls.py
From django.conf.urls import include, url
From django.contrib import admin
Urlpatterns = [
Url (r'^ polls/', include ('polls.urls'))
Url (r'^ admin/', admin.site.urls)
]
The include () function allows references to other URLconfs. Notice that the regular expression of the include () function does not have a $(string matching character), but a trailing slash. Whenever Django encounters include (), it excludes any part that matches that point and sends the remaining string to the accompanying URLconf for further processing.
The idea behind include () is to make plug-and-play URL easier. Because polls is in its own URLconf (polls/urls.py), they can be placed under "/ polls/", or under "/ fun_polls /", or under "/ content / polls/" or other path root directory.
6. When to use include ()
Include () when you include other URL formats, you should always use them. Admin.site.urls is the only exception.
It doesn't fit your opinion?
If you just see include (admin.site.urls) and admin.site.urls, you may be using a version of Django that does not match the version of this tutorial. You will need to switch to an older tutorial or a newer version of Django.
You have now connected the index view to URLconf. Let it verify its work and run the following command:
$python manage.py runserver
Go to http:// localhost:8000 / polls / in the browser, and you should see the text "Hello,world *".
7. The url () function passes four arguments, two required: regex and view, and two optional: kwargs, and name. At this point, it is worth examining these arguments.
Url () parameter: regular expression
The term "regular expression" is a commonly used short format, meaning "regular expression," which is the syntax used to match patterns in strings or, in this case, URL format. Django starts with the first regular expression and places it in the list, comparing the requested URL with each regular expression until a match is found.
Note that these regular expressions do not search for GET and POST parameters or domain names. For example, https://www.example.com/myapp/ myapp/ conf will look for URLs in the request. Https://www.example.com/myapp/?page=3 myapp/ conf also looks for URLs in the request.
If you need help with regular expressions, refer to the re module documentation. In fact, you don't need to be an expert on regular expressions, because you just need to know how to capture simple patterns. In fact, the lookup performance of complex regular expressions can be very poor, so you probably shouldn't rely on the full functionality of regular expressions.
Finally, a performance note: these regular expressions are compiled the first time the URLconf module is loaded. They're super fast.
Url () parameter: view
When Django finds a regular expression match, Django calls the specified view function, with one of the HttpRequest objects as the first parameter and any "capture" value from the regular expression as the other parameter. If the regular expression uses a simple capture, the value is passed as a positional parameter; if it uses a named capture, the value is passed as a keyword parameter. We will give an example later.
Url () parameter: kwargs
Any keyword parameter can be passed to the target view in the dictionary. We will not use this feature of Django in the tutorials.
Url () parameter: name
Naming your URL allows you to explicitly reference it from elsewhere in the Django, especially in the template. This powerful feature allows you to completely change the URL mode of your project while touching only a single file.
8. Django official documents:
Https://docs.djangoproject.com/en/1.11/
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.