django 2.x 和 django 1.x 的差异

app的urls.py

1.x:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('bouncer.views',
# Application installation url.
url(r'^app/(?P<app_id>[abcdef\d]{32})/$', 'checkOS'),
url(r'^studentapp/(?P<app_id>[abcdef\d]{32})/$', 'checkOS'),
url(r'^edu/(?P<app_id>[abcdef\d]{32})/$', 'adminTerms'),
)

2.x:

from django.contrib import admin
from django.urls import re_path
from . import views
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = [
# Application installation url.
re_path(r'^app/(?P<app_id>[abcdef\d]{32})/$', views.checkOS),
re_path(r'^studentapp/(?P<app_id>[abcdef\d]{32})/$', views.checkOS),
re_path(r'^edu/(?P<app_id>[abcdef\d]{32})/$', views.adminTerms),
]

app的views.py

python2.x 

import urlparse 

from urllib2 import HTTPBasicAuthHandler, HTTPPasswordMgr
from urllib2 import build_opener, Request
from ConfigParser import SafeConfigParser as ConfigParser
from urllib2 import quote

python3.x 

from urllib import parse
from urllib.parse import urlparse

from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgr
from urllib.request import build_opener, Request
from configparser import SafeConfigParser as ConfigParser
from urllib.parse import quote
from urllib.parse import urlparse

urllib.unquote(urlparse.urlparse(target_url).path)  ---->   urllib.parse.unquote(urlparse(target_url).path)

urlparse.urlparse(connection).netloc  ---->   urlparse(connection).netloc

render_to_response(info['template'],RequestContext(request, info['context'])) ---->  render_to_response(info['template'],info['context']))     info['context'])是字典

render_to_response('bouncer/unknown_os.html',RequestContext(request, templatedata)   ---->    render(request,'bouncer/unknown_os.html',templatedata)    渲染用render(request, "html", "rcontext字典")

project 的 settings.py

1.x:

MIDDLEWARE_CLASSES = (
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
join(dirname(dirname(__file__)), 'templates')
)

2.x:

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [join(dirname(dirname(__file__)), 'templates'),
join(dirname(dirname(__file__)), 'bouncer/templates'),],
'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',
'django.template.context_processors.static',
'django.template.context_processors.i18n',
],
},
},
]

猜你喜欢

转载自www.cnblogs.com/tuijin/p/9479984.html