PYTHON January 04, 2020

django模板语言

Words count 2.3k Reading time 2 mins.

一、 { {}} 获取render字典

如views.py

def index(request):
    return render(request, 'index.html', {...
Read article

PYTHON August 20, 2018

linux下django+vue.js环境搭建及项目构建

Words count 17k Reading time 16 mins.

python: 2.7.5

django: 1.11.7

node: 8.5.0

mysql: 5.7

1. 安装python

centos下默认已经安装了python

2. 安装 node.js

下载安装包 版本地址

wget https://nodejs.org/dist/v8.5.0/node-v8.5.0-linux-x64.tar.gz

解压

tar zxvf node-v8.5.0-linux-x64.tar.gz

配置环境变量/etc/profile

export NODE_HOME...
Read article

PYTHON August 20, 2018

django返回对象Response

Words count 4.9k Reading time 4 mins.

https://www.cnblogs.com/huwei934/p/6978641.html

不使用模板,直接返回数据,如:

def test_response(request):
    return HttpResponse('this is response test!')

from django.template import loader

def test_response(request):
    t1 = loader.get_template('index.html...
Read article

PYTHON August 20, 2018

Django 国际化

Words count 7.4k Reading time 7 mins.

django版本:

>>> import django
>>> print django.VERSION
(1, 11, 5, u'final', 0)
>>> 

python版本:

[root@new_name ~]# python -V
Python 2.7.5

一、 构建Django项目

  1. 创建项目
django-admin startproject myproject

目录结构:

  1. 进入项目根目录,创建...
Read article

PYTHON August 20, 2018

djangoAjax请求

Words count 6k Reading time 5 mins.

Django 中自带了防止CSRF攻击的功能,所以在表单或者Ajax发送POST的请求的时候,如果没有带有csrf_token ,请求会被拒绝

GET请求不需要csrf认证,POST请求需要认证才能得到正确的结果。一般在POST表单中加入

{% csrf_token%}
<form method="POST" action="/post-url/">
    {% csrf_token %}
    <input...
Read article

PYTHON August 19, 2018

all()、filter()、get()、values()、values_list()、区别

Words count 5.8k Reading time 5 mins.

我们在models.py里定义一个Student类,如:

class Student(models.Model):
    stu_name = models.CharField(max_length=30)
    stu_no = models.CharField(max_length=20)
    stu_sex = models.CharField(max_length=10)
    stu_age = models.IntegerField()
    stu_birth = models.DateTimeField(auto_now_ad...
Read article

PYTHON August 19, 2018

djang获取字段方法

Words count 4.9k Reading time 4 mins.

以Student这个model为例

In [59]: Student._meta.fields
Out[59]: 
(<django.db.models.fields.AutoField: id>,
 <django.db.models.fields.CharField: stu_name>,
 <django.db.models.fields.CharField: stu_no>,
 <django.db.models.fields.CharField: stu_sex...
Read article

PYTHON August 19, 2018

django配置URL

Words count 1.3k Reading time 1 mins.

之前配置url是直接修改url.py如下图:

from django.conf.urls import url
from django.contrib import admin
import blog.views as bv
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', bv.index)
]

这样是可行的,但是如果我们的网站过于庞大,会有好多应用,而且url也很多。这时候,我...

Read article

PYTHON August 19, 2018

django在linux上搭建需要注意的问题

Words count 191 Reading time 1 mins.

启动runserver时需要在命令行后添加 地址和端口号,如下:

python manage.py runserver 0.0.0.0:8000

修改settings文件,允许访问的主机参数:

ALLOWED_HOSTS = ['*']
Read article

PYTHON August 19, 2018

django增删改查

Words count 7.3k Reading time 7 mins.

models.py

class Student(models.Model):
    stu_name = models.CharField(max_length=30)
    stu_no = models.CharField(max_length=20)
    stu_sex = models.CharField(max_length=10)
    stu_age = models.IntegerField()
    stu_birth = models.DateTimeField(auto_now_add=True)

    def...
Read article
Load more
0%