且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Chapter 3:Code Style in Django

更新时间:2022-08-12 10:41:33

• Django coding style
• Using IDE for Django web development
• Django project structure
• Best practices—using version control
• Django rescue team (where to ask Django questions)
• Faster web development—using Twitter-Bootstrap

The Django project structure
$ django-admin.py startproject django_mytweets
Chapter 3:Code Style in Django
Paste_Image.png

manage.py: This is utility script is used to manage our project. You can think of it as your project's version of django-admin.py. Actually, both django-admin.py and manage.py share the same backend code.

settings.py 参数说明:

  • DEFAULT_APPS: This parameter contains the default Django installedapplications (such as the admin)
  • THIRD_PARTY_APPS: This parameter contains other application likeSocialAuth used for social authentication

  • LOCAL_APPS: This parameter contains the applications that arecreated by you

Best practices - using version control

Let's take a look at the following commands and their uses(git常用命令):

  • $git add <file-name> or $git add: For adding all files to the staging area in bulk.
  • $git status: To know the status of your working directory, which files have been added, and which files have not been added.
  • $git diff: To get the status of what is modified and staged, or what is modified and has not been staged.
  • $ git commit -m: To commit the changes made, first you have to add them to the staging area; then, you have to commit them using this command.
  • $ git rm <file-name>: If you have mistakenly added any file to the staging area, you can remove it from the staging area by using this command.
  • $git stash: Git doesn't track the renamed files. In other words, if you have renamed already staged files, you will have to add them again to the staging and then commit. You can save the changes by not actually committing to the repository by using the following command.
  • $git stash apply: It takes all the current changes and saves it to the stack. Then, you can continue working with your changes. Once you are in a position to get your saved changes, you can do so using this command.

分支

  • $git branch: To list an existing branch using Git, we need to use this command.
Chapter 3:Code Style in Django
Paste_Image.png
  • $git checkout -b <new-branch-name>: A new branch can be created in the existing repository using this command. We can see logically how it looks with the help of the following block diagram:
Chapter 3:Code Style in Django
Paste_Image.png

You will get a message informing you that you have switched to the new branch. If you want to switch back to the old branch, you can use the following command:

  • $git checkout <old-branch-name>: You will see the message Switched to branch <old-branch-name>.

  • $git merge <branch-name>: After the feature is complete, you can merge it to the branch of your choice using this command. This will merge the
    branch <branch-name> to your current branch. To sync the changes back to
    the <branch-name>, you can check out from your current branch to the
    branch <branch-name> and merge again. You can also mark the important points in your commit history by using tags.

  • After the commit, you can tag an important commit by using the $git tag-a v1.0 command.

  • To get new changes from the remote server, you can fetch the changes from Git using the $git fetch command.

  • To merge the changes directly to your current branch, you can use the $git pull command.

  • After you are done with your changes, you can commit and push them to the remote repository using the $git push command.

运行服务,切换到根目录下执行如下语句:

$ python3 manage.py runserver 8080