longgb246的博客


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • 公益404

  • 搜索
close
longgb246的博客

Tutorial_7 Customizing the admin site

发表于 2017-04-23 | 分类于 Python |

一、管理admin

1、修改顺序

1
2
3
4
5
6
7
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question_text']
admin.site.register(Question, QuestionAdmin)

使得pub_date显示在question_text前面。

阅读全文 »
longgb246的博客

Tutorial_6 Static files

发表于 2017-04-23 | 分类于 Python |

一、静态文件

创建static在app的目录下,polls/static/。和建立templates类似,还需要在建立app名字命名的文件夹。将文件放于其下polls/static/polls/。

1.1 添加css

建立css文件夹,再建立style.css,写入:

1
2
3
li a {
color: green;
}

修改index.html

1
2
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/css/style.css' %}" />

阅读全文 »
longgb246的博客

Tutorial_5 Testing

发表于 2017-04-23 | 分类于 Python |

一、测试

写测试类,在app的tests.py文件下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import datetime
from django.utils import timezone
from django.test import TestCase
from .models import Question
class QuestionMethodTests(TestCase):
def test_was_published_recently_with_future_question(self):
"""
was_published_recently() should return False for questions whose
pub_date is in the future.
"""
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(), False)

在终端运行。

阅读全文 »
longgb246的博客

Tutorial_4 Forms and generic views

发表于 2017-04-23 | 分类于 Python |
1
2
3
4
5
6
7
8
9
10
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
<input type="submit" value="Vote" />
</form>

其中forloop.counter是指for循环了多少次。

阅读全文 »
longgb246的博客

Tutorial_3 Views and templates

发表于 2017-04-23 | 分类于 Python |

一、视图

添加更多的视图,在app的views.py下:

1
2
3
4
5
6
7
8
9
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)

阅读全文 »
12…11
longgb246

longgb246

这个人太懒了,什么都没有留下...

52 日志
9 分类
24 标签
RSS
GitHub 微博 简书 CSDN 经管之家
我的好友
  • 大抹茶君
© 2017 longgb246
由 Hexo 强力驱动
主题 - NexT.Pisces