Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Django1.9 develop web projects

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how Django1.9 develops web projects". In daily operation, I believe many people have doubts about how Django1.9 develops web projects. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how Django1.9 develops web projects". Next, please follow the editor to study!

Python Version: 2.7.10

Django Version: 1.9

REST framework 3.3.2

Create a project:

Django-admin startproject TestWeb

Switch to the project to create an application:

Django-admin startapp app01

Modify the configuration file settings.py to add your application app01 and rest_framework

INSTALLED_APPS = [

'django.contrib.admin'

'django.contrib.auth'

'django.contrib.contenttypes'

'django.contrib.sessions'

'django.contrib.messages'

'django.contrib.staticfiles'

'app01'

'rest_framework'

]

Create a templates folder under the app01 folder and put it into the index.html template file

Tell your application template file path and modify settings.py

TEMPLATE_DIRS=os.path.join (os.path.dirname (_ _ file__), 'templates')

The latest version 1.9 does not need to be configured. It will be found in the templates folder of the application by default.

TEMPLATES = [

{

'BACKEND': 'django.template.backends.django.DjangoTemplates'

'DIRS': []

'APP_DIRS': True

'OPTIONS': {

'context_processors': [

'django.template.context_processors.debug'

'django.template.context_processors.request'

'django.contrib.auth.context_processors.auth'

'django.contrib.messages.context_processors.messages'

]

}

}

]

Configure the urls.py file

Urlpatterns = [

Url (r'^ admin/', admin.site.urls)

Url (r'^ $', index)

]

Launch your application

Python manage.py runserver 8080

back-stage management

The default database configuration is sqlite,mysql. The configuration is as follows

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql'

'USER':'root'

'PASSWORD':''

'NAME':'test'

'HOST':'localhost'

# 'ENGINE':' django.db.backends.sqlite3'

# 'NAME': os.path.join (BASE_DIR,' db.sqlite3')

}

}

Localhost:8080/admin/ login report no such table: auth_user error

Run python manage.py syncdb to automatically create the required tables

Note: python manage.py syncdb failure of Django 1.7.1 and above requires the following command

Python manage.py makemigrations

Python manage.py migrate

Create a model, modify models.py

From _ _ future__ import unicode_literals

From django.db import models

# Create your models here.

Class Mysite (models.Model):

Title=models.CharField (max_length=100)

Url=models.URLField ()

Author=models.CharField (max_length=100)

Num=models.IntegerField ()

Def _ unicode__ (self):

Return self.title

Class Meta: # the query will be sorted by num

Ordering= ['num']

Python manage.py shell enters interactive data query

From app01.models import *

M=Mysite (title='django',num=2)

M.save ()

M=Mysite.objects.all ()

M [0] .title

M=Mysite.objects.get (num=2)

M=Mysite (title='django',num=3)

M.save ()

M=Mysite (title='django',num=9)

M.save ()

M=Mysite.objects.all (). Order_by ('num') # ascending order

M=Mysite.objects.all (). Order_by ('- num') # descending order

M.delete ()

M=Mysite.objects.all () [0:2]

The management interface manages your model admin.py

From django.contrib import admin

From app01.models import *

# Register your models here.

Admin.site.register (Mysite)

Use the form to display CSRF verification failed. Request aborted. Error

Solution:

[http://www.91pen.net/slove-the-problem-when-the-django-form-submitted-occur-403-error-csrf-verification-failed-request-aborted.html](http://www.91pen.net/slove-the-problem-when-the-django-form-submitted-occur-403-error-csrf-verification-failed-request-aborted.html)

Urls.py configuration

From django.conf.urls import url

From django.contrib import admin

From app01.views import *

Urlpatterns = [

Url (r'^ admin/', admin.site.urls)

Url (r'^ $', index)

Url (r'^ hello/$', hello)

]

Static resource file configuration

STATIC_URL ='/ static/'STATICFILES_DIRS= ('DGRAPH TestWebGripple staticwebsite,) at this point, the study on "how Django1.9 develops the web project" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report