STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
mysite/static/myapp/
/static/my_app/myexample.jpg
{ % load static from staticfiles %} <img src="{ % static "my_app/myexample.jpg" %}" alt="My image"/>
from django.http import HttpResponse class MyException(): def process_exception(request,response, exception): return HttpResponse(exception.message)
MIDDLEWARE_CLASSES = ( 'test1.myexception.MyException', ... )
pic=models.ImageField(upload_to='cars/')
pip install Pillow==3.4.1
MEDIA_ROOT=os.path.join(BASE_DIR,"static/media")
<html> <head> <title>文件上傳</title> </head> <body> <form method="post" action="upload/" enctype="multipart/form-data"> <input type="text" name="title"><br> <input type="file" name="pic"/><br> <input type="submit" value="上傳"> </form> </body> </html>
from django.conf import settings def upload(request): if request.method == "POST": f1 = request.FILES['pic'] fname = '%s/cars/%s' % (settings.MEDIA_ROOT,f1.name) with open(fname, 'w') as pic: for c in f1.chunks(): pic.write(c) return HttpResponse("ok") else: return HttpResponse("error")
python manage.py createsuperuser
而後按提示填寫用戶名、郵箱、密碼
from django.contrib import admin from models import * admin.site.register(HeroInfo)
class HeroAdmin(admin.ModelAdmin): ...
admin.site.register(HeroInfo,HeroAdmin)
@admin.register(HeroInfo) class HeroAdmin(admin.ModelAdmin):
class HeroAdmin(admin.ModelAdmin): actions_on_top = True actions_on_bottom = True
在models.py文件中 from django.db import models from tinymce.models import HTMLField from django.utils.html import format_html class HeroInfo(models.Model): hname = models.CharField(max_length=10) hcontent = HTMLField() isDelete = models.BooleanField() def hContent(self): return format_html(self.hcontent) 在admin.py文件中 class HeroAdmin(admin.ModelAdmin): list_display = ['hname', 'hContent']
在models.py中HeroInfo類的代碼改成以下: def hContent(self): return format_html(self.hcontent) hContent.admin_order_field = 'hname'
在models.py中爲HeroInfo類增長方法hName: def hName(self): return self.hname hName.short_description = '姓名' hContent.short_description = '內容' 在admin.py頁中註冊 class HeroAdmin(admin.ModelAdmin): list_display = ['hName', 'hContent']
class HeroAdmin(admin.ModelAdmin): ... list_filter = ['hname', 'hcontent']
class HeroAdmin(admin.ModelAdmin): ... list_per_page = 10
class HeroAdmin(admin.ModelAdmin): ... search_fields = ['hname']
class HeroAdmin(admin.ModelAdmin): ... fields = [('hname', 'hcontent')]
class HeroAdmin(admin.ModelAdmin): ... fieldsets = ( ('base', {'fields': ('hname')}), ('other', {'fields': ('hcontent')}) )
class HeroInline(admin.TabularInline): model = HeroInfo class BookAdmin(admin.ModelAdmin): inlines = [ HeroInline, ]
'DIRS': [os.path.join(BASE_DIR, 'templates')],
from django.core.paginator import Paginator def pagTest(request, pIndex): list1 = AreaInfo.objects.filter(aParent__isnull=True) p = Paginator(list1, 10) if pIndex == '': pIndex = '1' pIndex = int(pIndex) list2 = p.page(pIndex) plist = p.page_range return render(request, 'booktest/pagTest.html', {'list': list2, 'plist': plist, 'pIndex': pIndex})
url(r'^pag(?P<pIndex>[0-9]*)/$', views.pagTest, name='pagTest'),
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <ul> {%for area in list%} <li>{{area.id}}--{{area.atitle}}</li> {%endfor%} </ul> {%for pindex in plist%} {%if pIndex == pindex%} {{pindex}} {%else%} <a href="/pag{{pindex}}/">{{pindex}}</a> {%endif%} {%endfor%} </body> </html>
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
class AreaInfo(models.Model): aid = models.IntegerField(primary_key=True) atitle = models.CharField(max_length=20) aPArea = models.ForeignKey('AreaInfo', null=True)
python manage.py makemigrations
python manage.py migrate
from django.shortcuts import render from django.http import JsonResponse from models import AreaInfo def index(request): return render(request, 'ct1/index.html') def getArea1(request): list = AreaInfo.objects.filter(aPArea__isnull=True) list2 = [] for a in list: list2.append([a.aid, a.atitle]) return JsonResponse({'data': list2}) def getArea2(request, pid): list = AreaInfo.objects.filter(aPArea_id=pid) list2 = [] for a in list: list2.append({'id': a.aid, 'title': a.atitle}) return JsonResponse({'data': list2})
from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index), url(r'^area1/$', views.getArea1), url(r'^([0-9]+)/$', views.getArea2), ]
from django.conf.urls import include, url from django.contrib import admin urlpatterns = [ url(r'^', include('ct1.urls', namespace='ct1')), url(r'^admin/', include(admin.site.urls)), ]
'DIRS': [os.path.join(BASE_DIR, 'templates')],
<!DOCTYPE html> <html> <head> <title>省市區列表</title> </head> <body> <select id="pro"> <option value="">請選擇省</option> </select> <select id="city"> <option value="">請選擇市</option> </select> <select id="dis"> <option value="">請選擇區縣</option> </select> </body> </html>
<script type="text/javascript" src="static/ct1/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript"> $(function(){ $.get('area1/',function(dic) { pro=$('#pro') $.each(dic.data,function(index,item){ pro.append('<option value='+item[0]+'>'+item[1]+'</option>'); }) }); $('#pro').change(function(){ $.post($(this).val()+'/',function(dic){ city=$('#city'); city.empty().append('<option value="">請選擇市</option>'); $.each(dic.data,function(index,item){ city.append('<option value='+item.id+'>'+item.title+'</option>'); }) }); }); $('#city').change(function(){ $.post($(this).val()+'/',function(dic){ dis=$('#dis'); dis.empty().append('<option value="">請選擇區縣</option>'); $.each(dic.data,function(index,item){ dis.append('<option value='+item.id+'>'+item.title+'</option>'); }) }) }); }); </script>