Django learning record first day, django first day

Source: Internet
Author: User
Tags install django pip install django

Django learning record first day, django first day

From this article, I will record my Django learning process.

Day-1

I

The first step is to install Django. I chose Django1.11. Why choose 1.11, because it is a long-term technical support version (LTS). The official website will add new features and fix some bugs.

Now, the version has been selected and it is time to install it. There are two ways to install it here:

1.Open CMD and install Python package management tool pip:

Pip install Django = 1.11.11 (Write the Django version you want to use later. If you don't write it, the highest version is installed by default .)

2.Open PyCharm and find the settings.

Click the green plus sign on the Right To download the plug-in you want to download.

 

PS: If you have installed Django but it is not your ideal version, enter the command: pip uninstall Django in CMD (you do not need to enter the version number later, because whatever version you release will be uninstalled .)

PyCharmIf you can install Django, you can also uninstall it, such as minus signs on the right. Select the plug-in you want to uninstall and click minus signs to uninstall the plug-in.

II

You can also create a Django project by using command line (CMD) and PyCharm.

1.Use CMD to create:

Switch to the directory where you want to save the Django Project

Run the django-admin startproject project name

2.Create with PyCharm:

Create a project, select Django, and select the path Create to O98K.

3.

Start the Django project, which can also be started through the command line or PyCharm.

1.Run CMD to start:

Switch to the Django project and run python manage. py runserver IP: the port (local port 8000 by default) is enabled without any objective factors. If you want to quit, press Ctrl + C on the Windows system and use Commend + C on the Mac system.

2.Start with PyCharm:

It's easy. Click the green triangle above to start it. Pay attention to this little triangle here. The root directory name is your project name, the drop-down menu on the left of the triangle is also your project name.

Thu

DjangoProject Settings

1: Open setting and find out the line with csrf In the MIDDLEWARE comment. It should be 46 lines. It seems that this line has something to do with MIDDLEWARE and will be used later. Now, you need to comment it out first.

2: Enable urls to change the bottom URL patternsUrl (R' ^ index /', Index), this index is random, and can be any name, and then define an index function, which is used hereThree sets of new users:

FromDjango. conf. urlsImportUrl
FromDjango. contribImportAdmin
FromDjango. shortcutsImportHttpResponse, render

Here we will talk aboutHttpResponse,What are you writing with this? What are you replying to on the webpage?

DefIndex (request ):
Return HttpResponse ("O98K ")

Start the project webpage (add index/after the url) to display the entered stuff:

If you want to return to a webpage, you can use the render method here. This method will help you open a file, read the content, and then return the result.

After that, you will create your webpage in templates. Here we will talk about it. If the project is created using CMD, it will not automatically create the templates folder for you, if you use a project created by PyCharm, it will automatically create a templates folder for you. Write a webpage and change the index function:

Def index (request ):
# Return HttpResponse ("O98K ")
Return render (request, "index.html ")

Here the problem comes. Django does not know where to find the html file. here we need to set it. Open setting and find the DIRS under TEMPLATES and it is blank, here, we need to set the path. Here, the path cannot be written to death, because it is configured with relative paths. Write OS here. path. join (BASE_DIR, 'templates') to find the templates. If you use a project created by PyCharm, depending on the PyCharm version, he will automatically fill in the templates search path or write the templates search path at the bottom of setting. After all, he will automatically create templates for you, it's not easy for anyone to do it --.

This time the path is set ~ Now I can find my webpage:

Now, I want to return to the templates page where I copied my login.html, and then added a ing in the urls file:

Urlpatterns = [
# Url (R' ^ admin/', admin. site. urls ),
Url (R' ^ index/', index ),

Url (R' ^ login/', login)
]

The above also adds a login function:

Def login (request ):
Return render (request, "login.html ")

After all the settings are complete, start the project and find that the style of my logon interface is gone. I think this is because the bootstrap and jQuery used in my logon interface are not loaded.

Now that I want to configure static files, I need to create another file to store my static files. Although the folder name is random, however, I still got the name of a comparative semantic text static for ease of use. The above templates path needs to be configured, and the static path here also needs to be configured. Open setting and write it at the end:

 

After that, copy the static files such as bootstrapand jqueryto my staticand Add the/static/prefix to the import path of my login.html. Then, my login interface will be perfectly displayed:

 

This is where we are today. I learned this on the first day and wrote my blog for the first time. I hope there will be more people to join us. Thank you !!

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.