Python Lover(2)Django Doc - URL Mapping - View - Template
1. Admin Console
Creating an admin user
>python manage.py createsuperuser
Username (leave blank to use 'carl'): admin Email address: luohuazju@gmail.com Password: Password (again): Superuser created successfully.
Visit this page to login in
http://localhost:8000/admin/
Admin is enabled and we can CURD the group and user there.
Add our app polls into admin in admin.py
from django.contrib import admin
from polls.models import Question
# Register your models here.
admin.site.register(Question)
Refresh the page, then I can see the changes there. And there are some ways to customized the forms in admin. But I thought it was not that important.
Adding related objects
Add Choice Registered in the admin.py
from django.contrib import admin
from polls.models import Question, Choice
# Register your models here.
admin.site.register(Question)
admin.site.register(Choice)
Customized the Question Related Choice
from django.contrib import admin
from polls.models import Question, Choice
# Register your models here.
#class ChoiceInline(admin.StackedInline):
class ChoiceInline(admin.TabularInline):
model = Choice
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
admin.site.register(Question, QuestionAdmin)
Order and Display
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published Recently?'
Filter
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
Search Part
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question_text', 'pub_date', 'was_published_recently')
list_filter = ['pub_date']
search_fields = ['question_text']
2. Front View
View —Action
Template —— HTML
Write First View
Something like writing PHP, haha.
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("This is the index page.”)
Url Mapping in app polls
polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
System URL Mapping
easypoll/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'easypoll.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
It works after we visit this page
http://localhost:8000/polls/
Write More Views
def index(request):
return HttpResponse("This is the index page.")
def detail(request, question_id):
return HttpResponse("You are looking at question %s." % question_id)
def results(request, question_id):
response = "You are looking at the results of question %s."
return HttpResponse(response % question_id)
def vote(request, question_id):
return HttpResponse("You are voting on question %s." % question_id)
More Mapping
urlpatterns = patterns('',
#/polls/
url(r'^$', views.index, name='index'),
#/polls/5/
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
#/polls/5/results/
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
#/polls/5/vote/
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
)
Make the Views Working
This will display all the Questions There
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
#latest 5 records
output = ', '.join([p.question_text for p in latest_question_list])
return HttpResponse(output)
But we need to customized the html from my understanding.
Using HTML Template
from django.http import HttpResponse
from polls.models import Question
from django.template import RequestContext, loader
# Create your views here.
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
#latest 5 records
template = loader.get_template('polls/index.html')
context = RequestContext(request, {
'latest_question_list': latest_question_list,
})
return HttpResponse(template.render(context))
easypoll/polls/templates/polls/index.html
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
Using Render
from django.shortcuts import render
# Create your views here.
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
#latest 5 records
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
How to Do the Detail
from django.http import Http404
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404
return render(request, 'polls/detail.html', {'question': question})
Just a very simple page polls/templates/polls/detail.html
{{ question }}
Shortcut
from django.shortcuts import get_object_or_404
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'polls/detail.html', {'question':question})
<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>
Change the URL in index.html page as follow:
<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>
Namespace the URL Names
In the main urls.py
url(r'^polls/', include('polls.urls', namespace="polls")),
In the Index.html
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
References:
https://docs.djangoproject.com/en/1.7/intro/tutorial02/
deployment
http://sillycat.iteye.com/blog/582074
- 浏览: 2560417 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
Stop Update Here
2020-04-28 09:00 322I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 484NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 374Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 375Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 345Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 436Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 444Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 381Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 463VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 394Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 487NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 432Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 342Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 255GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 456GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 332GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 318Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 324Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 302Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 315Serverless with NodeJS and Tenc ...
相关推荐
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,亲测可用
《Python库django-aliyun-oss2-storage:阿里云OSS2存储服务的Django集成》 在Python的开发世界中,Django是一款强大的Web框架,而阿里云OSS(Object Storage Service)则是一个广泛使用的云存储服务。当这两者结合...
Python在线考试系统后端-大学毕业设计-基于Django+Django -Rest-Framework 适合大学生计算机专业的毕业设计,课程设计。 Python在线考试系统后端-大学毕业设计-基于Django+Django -Rest-Framework 适合大学生...
资源分类:Python库 所属语言:Python 资源全名:Django-3.2.11-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
**Python库 django-template-model-1.0.3.tar.gz** 在IT行业中,Python是一种极其流行的高级编程语言,尤其在Web开发领域,它以其简洁、易读的语法和强大的库支持而备受青睐。在这个场景中,我们关注的是一个名为`...
《Python库深度解析:django-layers-hr-1.9》 在IT行业中,Python以其简洁易读的语法和强大的库支持,成为了后端开发的热门选择。在众多的Python库中,`django-layers-hr`是一个专为人力资源管理设计的Django应用,...
资源分类:Python库 所属语言:Python 资源全名:djangocms-cameraslider-0.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:django-cascading-dropdown-widget-0.2.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
`django-url-framework-0.3.3.tar.gz`提供了Django框架的一个关键部分,即URL路由系统,它是Django中用于构建清晰、灵活URL模式的关键工具。这个库使得开发者能够构建出优雅的URL结构,从而提高Web应用的用户体验...
资源分类:Python库 所属语言:Python 资源全名:django-datatables-view-1.12.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:django-url-framework-0.3.7.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:django-wkhtmltopdf-3.2.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:django-storage-url-0.2.0.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
django-python3-ldap, python 3的Django LDAP用户身份验证后端 django-python3-ldapdjango-python3-ldap 为 python 2和 3提供了一个 Django LDAP用户身份验证后端。特性使用LDAP服务器验证用户身份。将LDAP用户与...
在Python中,安装这样的库通常通过pip进行,命令可能是`pip install django-template-update-get-1.0.1`。一旦安装,可以在Django项目中导入并使用提供的功能,例如在视图函数中调用特定的模板更新或获取方法。 6....
支持python3.6Django2.0-goflow 工作流引擎 a
资源分类:Python库 所属语言:Python 资源全名:django-pyodbc-azure-1.2.10.zip 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
**Python库 Django-Python-Interpreter 0.0.1** `django-python-interpreter-0.0.1.tar.gz` 是一个包含Python库的压缩包,主要用于在Django框架内运行Python代码。Django是一个用Python编写的开源Web框架,它遵循...