Python & amp; PyCharm & amp; Django builds a web development environment, python3

Source: Internet
Author: User

Python & PyCharm & Django build web development environment, python3
1. Install software

1. install Python 2.7, PyCharm, pip (Python package management tool), and Django (pip install Django)


Ii. Deployment 1. After PyCharm creates a Django project, its directory is as follows: the subdirectory MyDjangoProject indicates the global configuration of the project, which is setttings. py, urls. py and wsgi. py, where setttings. py includes the system database configuration, application configuration, and other configurations, urls. py indicates the Url ing configuration of the web project. The subdirectory student is the app created under the project, including models. py, tests. py and views. py and other file templates directories are the template file directory manage. py is a management tool provided by Django that can synchronize databases. 2. After the database is created, it can be started properly. Click the Run button and an error is reported during startup:
 1 Traceback (most recent call last): 2   File "D:/workspace/MyDjangoProject/manage.py", line 10, in <module> 3     execute_from_command_line(sys.argv) 4   File "D:\Python27\lib\site-packages\django\core\management\__init__.py", line 338, in execute_from_command_line 5     utility.execute() 6   File "D:\Python27\lib\site-packages\django\core\management\__init__.py", line 312, in execute 7     django.setup() 8   File "D:\Python27\lib\site-packages\django\__init__.py", line 18, in setup 9     apps.populate(settings.INSTALLED_APPS)10   File "D:\Python27\lib\site-packages\django\apps\registry.py", line 89, in populate11     "duplicates: %s" % app_config.label)12 django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: admin
The admin configuration is conflicted. Open the setttings. py file and the admin configuration is repeated.
 1 INSTALLED_APPS = ( 2     'django.contrib.admin', 3     'django.contrib.auth', 4     'django.contrib.contenttypes', 5     'django.contrib.sessions', 6     'django.contrib.messages', 7     'django.contrib.staticfiles', 8     'django.contrib.admin', 9     'student',10 )
Comment out one of the lines (why is this problem? It is probably a bug), restart, OK 3. When the web Project is added to the page, we haven't written a line of code yet, the program is running duang! Add a Hello World page. Open the student/views. py file and enter the following content:
def sayHello(request):    s = 'Hello World!'    current_time = datetime.datetime.now()    html = 'Open url. in The py file, configure url ing: url (R' ^ student/', sayHello). When you enter http: // **/student, the sayHello method is called, this method uses HttpResponse () to return the page content as a response. Restart the service and access http: // localhost: 8000/student/in views. the py page can call the HttpResponse () class to return the elements required by the page to the browser in the form of strings. In this way, the page logic is mixed with the page, and handwriting is cumbersome and the workload is large. What should we do if we need to display some dynamic data while the page remains unchanged? For example, when a user accesses http: // localhost: 8000/student/, we want to dynamically display some student data. You can do this: first create a student.html file under the templates directory. The file is used as the template with the following content:
1 <! DOCTYPE html> 2 Modify the views. py file and add the showStudents () method ()
1 def showStudents(request):2     list = [{id: 1, 'name': 'Jack'}, {id: 2, 'name': 'Rose'}]3     return render_to_response('student.html',{'students': list})
The token binds list_to_response.txt to student.html on the template page. Add url ing, url (R' ^ showStudents/$ ', showStudents) Modify settings. py template configuration: 'dirs': [BASE_DIR + R' \ templates ']. Restart the service and access http: // localhost: 8000/showStudents, we can now bind some "dynamic" data to the template. But how to access the database? Obtain the required data from the database and display it on the page? First, install the database driver, that is, mysql_python. then configure the database connection:
 1 DATABASES = { 2     'default': { 3         'ENGINE': 'django.db.backends.mysql', 4         'NAME': 'student', 5         'USER': 'root', 6         'PASSWORD': '1234', 7         'HOST': '127.0.0.1', 8         'PORT': '3306', 9         #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),10     }11 }
After the configuration is complete, check whether the database configuration is correct. Run the manage. py shell command to enter the shell interaction interface: Input:
1 from django.db import connection2 cursor = connection.cursor()
If no error is reported, the configuration is correct. Create a model, open models. py, and define the model as follows:
1 class Student(models.Model):2     id = models.BigIntegerField3     name = models.CharField(max_length=20, default='a')
Call manage. py syncdb. Normally, after this step is completed, the model will maintain consistency with the database. However, in the test, the table is not created in the database after the command is successfully executed. In this case, perform the following operations: (1) comment out the models. py file code and execute manage. py makemigerations student. [And manage. py migerate -- fake] (2) Open the comment and run the [manage. py makemigerations student and] manage. py migerate commands The above two steps can be used properly.Add method in views. py: showRealStudents
1 def showRealStudents(request):2     list = Student.objects.all()3     return render_to_response('student.html', {'students': list})
Urls. py Add the ing url (R' ^ showRealStudents/$ ', showRealStudents) to restart the service and open the connection: http: // localhost: 8000/showRealStudents page. The output is normal. So far, with Django, you can operate databases, customize templates, and display data on the page.

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.