Francis'Blog

加油!奋斗!奥里给!


  • Home

  • Categories

  • About

  • Archives

  • Search

two-scoops-of-django

Posted on 2019-07-04 | In django

django学习书籍two scoop of django的心得整理

导入包时要用相对路径导入

不要这样做

1
2
3
4
5
6
7
8
9
10
# DON'T DO THIS!
# Hardcoding of the 'cones' package
# with implicit relative imports
from cones.models import WaffleCone
from cones.forms import WaffleConeForm
from core.views import FoodMixin

class WaffleConeCreateView(FoodMixin, CreateView):
model = WaffleCone
form_class = WaffleConeForm

应该这样做

1
2
3
4
5
6
7
8
9
10
11
# cones/views.py
from django.views.generic import CreateView

# Relative imports of the 'cones' package
from .models import WaffleCone
from .forms import WaffleConeForm
from core.views import FoodMixin

class WaffleConeCreateView(FoodMixin, CreateView):
model = WaffleCone
form_class = WaffleConeForm

包导入时注意顺序

1
2
3
4
5
6
7
8
9
10
11
# cones/views.py
from django.views.generic import CreateView

# Relative imports of the 'cones' package
from .models import WaffleCone
from .forms import WaffleConeForm
from core.views import FoodMixin

class WaffleConeCreateView(FoodMixin, CreateView):
model = WaffleCone
form_class = WaffleConeForm

用Cookiecutter生成django项目

命令行

1
cookiecutter https://github.com/pydanny/cookiecutter-django

使用多个设置文件

1
2
3
4
5
6
7
8

settings/
├── __init__.py
├── base.py
├── local.py
├── staging.py
├── test.py
├── production.py

1

然后如果要运行时的时候指定某个settings文件

1
python manage.py shell --settings=twoscoops.settings.local

使用抽象类继承类

Django笔记

Posted on 2019-04-10

教学视频地址Python Django Web Framework Full Course for Beginnners

创建的model需要在admin.py中注册

1
2
from .models import Product
admin.site.register(Product)

通过python shell添加模型

首先进入django的python shell

1
python manage.py shell

导入模型

1
from products.models import Product

查看已创建的Product对象

1
Product.objects.all()

创建一个新的对象

1
Product.objects.create(title='New product', description="another one", price="19.2", summary="sweet")

创建页面

创建app

1
python manage.py startapp pagess

在views.py中创建函数

1
2
3
4
5
6
from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def home_view(*args, **kwargs):
return HttpResponse("<h1>Hello</h1>")

在mysite中的urls.py中加入path

1
2
3
4
5
6
7
8
9
from django.contrib import admin
from django.urls import path

from pages import views ##注意引入pages中的views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home_view, name='home') #注意这里的home_view没有括号的也就是一个纯函数名
]

views中函数的request参数

1
2
3
4
5
def home_view(request, *args, **kwargs):
print(request)
print(request.user)
print(args, kwargs)
return HttpResponse("<h1>Hello</h1>")

我们这边设置request变量, 打印出来是<WSGIRequest: GET '/'>, 一个WSGIRequest,再打印出request.user, 结果是AnonymousUser

模板的使用

1
2
3
def home_view(request, *args, **kwargs):

return render(request, "home.html", {})

第一个参数是request, 第二个是模板的名称, 第三个是需要传入的参数字典

如果需要使用模板,必须在settings.py中设置模板路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},

将'DIRS'设置为根目录下的template目录就可以在这个目录下创建模板的html文件了

模板的继承

创建模板html文件

这里我们创建base.html

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Coding for Entrepreneurs is doing try django</title>
</head>
<body>
{% block content %}
replace me
{% endblock %}
</body>
</html>

注意,这里的block模块, 在子模板中再次引入可以替换block间的内容

1
2
3
{% block content %}
replace me 这里将会被代替
{% endblock %}

引入模板

这里我们在home.html中继续base.html

1
2
3
4
5
{% extends 'base.html' %}
{% block content %}
<h1> Hello world </h1>
<p>this is a template</p>
{% endblock %}

模板的include

创建需要include的模板, 这里我们创建navbar.html

1
2
3
4
5
6
7
<nav>
<ul>
<li>Brand</li>
<li>Contact</li>
<li>About</li>
</ul>
</nav>>

在base.html中include进去

1
{% include 'navbar.html' %}

模板的上下文

首先在views.py中添加上下文参数

1
2
3
4
5
6
7
def about_view(request, *args, **kwargs):
my_context = {
"my_text": "this is about me",
"my_number": 123,
"my_list": [1, 2, 3, 4],
}
return render(request, 'about.html', my_context)

在模板中使用格式化渲染

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{% extends 'base.html' %}
{% block content %}
<h1> About page </h1>
<p>this is a template</p>
<p>
{{ my_number }}
{{ my_text }}
</p>
<ul>
{% for item in my_list %}
<li>{{ forloop.counter }}---{{ item }}</li>
{% endfor %}
</ul>
{% endblock %}

filters

Django Template Filters

介绍几个重要的

save

1
2
3
4
5
6
context = {
'Hello': "你好",
"lala": "啦啦啦",
'my_list': [1, 2, 3, 5],
'my_html': '<h1>Hello World</h1>'
}

假设有一段html源代码被传入, 如果直接用调用是不行的,没有被渲染出来

需要

1
{{ my_html|safe}}

需要添加save filter才能诶渲染

app中的template

在app中建立template可以使得这个app成为一个插件,在哪里都可以用

在app中建立template文件夹

这里我们的app是product, 所以在product文件夹下建立template文件夹

在建立的template文件夹中建立一个名称为app名字的文件夹

这里我们建立的是/products/template/products

—products

——-template

————products

—————–product_detail.html


在设置中给’APP_DIRS’赋值为True

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]

APP_DIRS决定了Djang是否会去寻找app中的template

如果说根目录下的template和app中的template有相同的路径,那么Django会先去找根目录下的template中的模板

比如

app下的 1

根目录下 2

这样的情况下,在view.py中设置render模板的路径为’/products/product_detail.html’Django会先选择/mysite/template/products/product_detail.html, 而不是/mysite/products/product_detail.html

forms

我们需要用户上传数据,这就需要forms

在app根目录下创建forms.py

1
2
3
4
5
6
7
8
9
10
11
12
from django import forms

from .models import Product

class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = [
'title',
'description',
'price',
]

注意这边的files如果有一个是没有设置defaul、不能是空的,那么用户提交的数据将会不完整, 无法进入数据库,导致错误发生

为表单提交创建新的view

1
2
3
4
5
6
7
8
9
10
11
def product_create_view(request):
form = ProductForm(request.POST or None)
if form.is_valid():
form.save()

# rerender it, to clear the text
form = ProductForm()
context = {
'form': form
}
return render(request, "products/product_create.html", context)

其中

1
2
form.save()
form.ProductForm()

是保存了数据之后刷新表单, 将输入框中的数据清空

创建html模板文件

在app目录下的/template/products中创建product_create.html

1
2
3
4
5
6
7
8
{% extends 'base.html' %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
{% endblock %}

添加到路由中

1
path('create/', product_create_view, name="product_create"),

创建生的Form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

class RawProductForm(forms.Form):
# overwrite the basic attar
title = forms.CharField(label='MY Label', widget=forms.TextInput(
attrs={
'placeholder':"Your title"
}
))
description = forms.CharField(
required = False,
widget=forms.Textarea(
attrs={

"class":"new-lass two",
"rows":20,
"col":120,
}
)
)
price = forms.DecimalField()

效果将会是
6

Django POST 和 GET

https://docs.djangoproject.com/en/2.2/ref/request-response/

在view.py中定义的函数,第一个参数是request

当有post和get请求时, 返回的request中有GET和POST两种属性

其实他们两个都是QueryDict对象,可以通过request.POST.get() 和request.GET.get()方法获取参数

TensorFlow基础的CNN网络

Posted on 2019-04-04 | In TensorFlow

英文原文地址https://cv-tricks.com/tensorflow-tutorial/training-convolutional-neural-network-for-image-classification/

为了演示如何构建基于卷积神经网络的图像分类器,我们将构建一个6层神经网络,用于识别和分离狗的图像和猫的图像。我们将构建的这个网络是一个非常小的网络,您也可以在CPU上运行。传统的神经网络非常擅长进行图像分类,如果在CPU上接受过培训,则需要更多的参数并花费大量时间。但是,在这篇文章中,我的目标是向您展示如何使用Tensorflow构建真实世界的卷积神经网络,而不是参与ILSVRC。在我们开始使用Tensorflow教程之前,让我们先介绍一下卷积神经网络的基础知识。

CNN的基础知识

神经网络本质上是解决优化问题的数学模型。它们由神经元构成,是神经网络的基本计算单元。神经元接受输入(比如说x),对它做一些计算(比方说:用变量w乘以它并加上另一个变量b)来产生一个值(比如说; z = wx b)。该值被传递给称为激活函数(f)的非线性函数,以产生神经元的最终输出(激活)。激活功能有很多种。其中一种流行的激活函数是Sigmoid:

1

使用Sigmoid函数作为激活函数的神经元将被称为Sigmoid神经元。神经元根据激活函数被命名,激活函数有很多种,如RELU,TanH等(请记住这一点)。一个神经元可以连接到多个神经元,如下所示:

1

在此示例中,您可以看到权重是连接的属性,即每个连接具有不同的权重值,而偏差是神经元的属性。这是产生输出y的sigmoid神经元的完整过程:

3

Read more »

123…12
Francis Cheng

Francis Cheng

很惭愧,就做了一点微小的工作。

35 posts
14 categories
16 tags
© 2019 Francis Cheng
Powered by Hexo
Theme - NexT.Gemini