The Django framework is easy to use and is ideal for building personal blog sites. There are many tutorials online, mostly about command-line operation Django, here to share some of the latest tools for the Django development process, mainly pycharm too powerful, not a pity.
The first time to write technology development blog post, may not grasp the focus, detailed slightly may also be unbalanced, thanks to support.
Environment & Tools: Windows server, Pycharm 2016.2.1, Django 1.10, Python 2.7, MySQL Community 5.7.14
0, the configuration before the creation
Install MySQL and have the operation password set
1. Create the project
Create a new project in Pycharm, select the Django project, enter the path and application name, and create the project.
This is the initial interface after creation
2. Create and manipulate the database
Pycharm can create, read and write to the database, but if you want to connect to the project, or you need to use code in the Project , click the database Sidebar (location with personal preferences, default to the right), add database, (figure is to write the blog when the fill).
Fill in the connection information, the database column can be filled out the name of the specific databases, do not fill load all databases. The first time you use Pycharm will prompt to set the operation password, complete can click "Test Connection" testing connection situation, no problem click "OK", enter the operation password to complete the connection.
The left is the MySQL directory, if you modify, see not find their own database, click "More Schemes",
You can manipulate the database directly at the command line,
Double-click on the left "table1" to display the data table graphically.
3. Create database in the project, modify the configuration
So far as if we have not written code (the database that is to demonstrate pycharm powerful database operations), and the old version, in the modification of settings.py only need to modify the database connection configuration.
The settings.py database section is modified as follows:
DATABASES = { 'default': { 'ENGINE':'Django.db.backends.mysql', 'NAME':'ip_db', 'USER':'Root', 'PASSWORD':'Root', 'HOST':'localhost', 'PORT':'3306', }}
urls.py modified as follows:
from django.conf.urls import *from = [ url (r'^admin/') , Admin.site.urls),]
4, generate the Background management table
Under "terminal" below Pycharm, enter
Python manage.py Migrate
Next you need to create a super user's account password
python manage.py Shell from django.contrib.auth.models Import useruser=user.objects.create_superuser (' user name ' ,' mailbox ', ' password ')
So that we can enter the background management interface, click on the running program, in the browser input: 127.0.0.1:8000/admin
After logging in, you can create users, groups, manage permissions, modify passwords, and more.
5. Custom Display
Custom background display needs to be modified in models.py, you can view the official document for custom configuration, here is a brief introduction to several commonly used
#coding =utf-8this is to facilitate the compilation of Chinese from__future__ Import unicode_literals fromdjango.db Import models# Create your models here.#-*-coding:utf-8-*- fromdjango.db Import models# Create your models here.classPublisher (models. Model): State_choices=((U' Down', u'device is turned off'), (U' up', u'Device Boot'), (U'avi', u'This IP is not assigned'),) model_choices=((U'Fwq', u'Normal Server'), (U'GMSZX', u'Gaomi four sub-star'), (U'ups', u'UPS'), (U'Ccjd', u'Storage Node'), (U'others', u'other'),) IP= Models. Charfield (max_length= -, help_text='example:172.25.0.0', verbose_name='IP Address', unique=true) #unique =true if the item is globally unique and cannot be repeated state= Models. Charfield (max_length= -, verbose_name='Current status', choices=state_choices) #choices是以下拉菜单显示 MODEL= Models. Charfield (max_length= -, blank=true,verbose_name='Model', choices=model_choices) #verbose_name是网页的显示名称 IDD= Models. Charfield (max_length= -, blank=true,verbose_name='Device number') #blank =trueYou may not fill in the USER= Models. Charfield (max_length= -, blank=true,verbose_name='Device User') TEL= Models. Charfield (max_length= -, verbose_name='Contact Information', blank=True) LOC= Models. Charfield (max_length= -, blank=true,verbose_name='Device Location') NOTE= Models. Charfield (max_length= -, verbose_name='Notes', blank=True) time= Models. Datetimefield (auto_now_add=True) IPMI=models. Charfield (max_length= -, help_text='example:172.25.0.0', verbose_name='IPMI Address', unique=true,blank=True)classmeta:verbose_name_plural='IP Query'#这是表的网页显示名称 Verbose_name='IP'
You need to update the database below
Enter the following command in the terminal terminal
Python manage.py Makemigrationspython manage.py Migrate
Finally, modify the configuration in admin.py
fromdjango.contrib Import admin# Register your models here. fromdjango.contrib.auth.models Import User fromipapp.models Import publisher# fromipapp.models Import Publisher, Author, Bookclasspublisheradmin (admin. Modeladmin): Search_fields= ('IP',' State','MODEL','USER') #根据属性搜索 List_display=('IP',' State','USER','TEL') #列表显示的属性 List_filter= (' State',) #筛选 passadmin.site.register (publisher,publisheradmin)
Effect
After modifying the model all need to update the database Makemigrations command, but always error, not yet, so I am modifying the models, such as adding columns, the database command line directly with the ALTER command to add, This will not require the use of the Makemigrations command.
Pycharm+django+python+mysql developing a background management database