最近在学django blogy example 里面有一些blog.css、base.css、boo

chongeryan 的BLOG
用户名:chongeryan
文章数:23
访问量:4727
注册日期:
阅读量:5863
阅读量:12276
阅读量:311651
阅读量:1026749
51CTO推荐博文
26.用Django-Registration-Redux进行用户校验
Django中,有好多现成的应用提供了注册、登录、校验等功能,我们只要稍稍改动一下URL映射、视图和模板就可以使用它们。在这一章,我们将介绍用Django-Registration-Redux,顺便学习一下如何把外部的应用加到我们的项目中。
(1)安装Django-Registration-Redux
在Dos命令提示符下转到Python的Scripts文件夹,然后运行如下命令:
pip install django-registration-redux
正确安装的话会你看到“Successfully installed django-registration-redux”这样的提示。
打开settings.py文件,将INSTALLED_APPS改为:
rangoproject/settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration', # add in the registration package
同时在settings.py文件尾部加入以下内容:
rangoproject/settings.py:
REGISTRATION_OPEN = True
# 此项设置为True,用户方能注册
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = True
# 此项设置为True,用户可以自动登录
LOGIN_REDIRECT_URL = '/rango/'
# 用户登录后转向的页面
LOGIN_URL = '/accounts/login/'
# 用户未成功登录时转向的页面
(3)修改URL映射
修改rangoproject下的urls.py文件,改成下面这样:
rangoproject/urls.py:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')),
url(r'^accounts/', include('registration.backends.simple.urls')),
(4)设置模板
我们先来做登录模板.我们在templates下面新建一个叫registration的文件夹,下面说到的这些模板都放在这里。在这个文件夹下新建一个叫login.html的文件,加入如下内容:
templates/registration/login.html:
{% extends &base.html& %}
{% block body_block %}
&h1&登录&/h1&
&form method=&post& action=&.&&
{% csrf_token %}
{{ form.as_p }}
&input type=&submit& value=&登录& /&
&input type=&hidden& name=&next& value=&{{ next }}& /&
&p&还不是会员? &a href=&{% url 'registration_register' %}&&Register&/a&!&/p&
{% endblock %}
接着我们来做注册模板,在registration文件夹下新建一个叫registration_form.html的文件,加入如下内容:
templates/registration/registration_form.html:
{% extends &base.html& %}
{% block body_block %}
&h1&注册&/h1&
&form method=&post& action=&.&&
{% csrf_token %}
{{ form.as_p }}
&input type=&submit& value=&提交& /&
{% endblock %}
我们接下来做注册完成模板,在registration文件夹下新建一个叫registration_complete.html的文件,加入如下内容:
templates/registration/registration_complete.html:
{% extends &base.html& %}
{% block body_block %}
&h1&注册成功&/h1&
&p&您已成功注册!&/p&
{% endblock %}
然后是注销模板,在registration文件夹下新建一个叫logout.html的文件,加入如下内容:
templates/registration/logout.html:
{% extends &base.html& %}
{% block body_block %}
&h1&注销&/h1&
&p&您已退出当前登录.&/p&
{% endblock %}
准备好上面这些,我们可以开始体验了。在浏览器地址栏中输入“http://127.0.0.1/accounts/register”,感受一下全新的注册、登录、注销流程吧!
(5)修改链接
最后我们要调整一下base.html中的相关链接:
将注册链接改为: &a href=&{% url 'registration_register' %}&&
将登录链接改为: &a href=&{% url 'auth_login' %}&&
将注销链接改为:&a href=&{% url 'auth_logout' %}?next=/rango/&&
注意在注销链接中,我们加了一句”?next=/rango/&“,它的作用是在用户注销后,将其引导回Rango首页。
(6)修改注册流程
在当前的设置中,当用户完成注册时,程序会将其引导到”注册成功“页面。这样做感觉傻傻的,所以,我们来调整一***册流程。
我们来重写一下”registration.backends.simple.views“提供的RegistrationView。
修改rangoproject下的urls.py文件,改成下面这样:
rangoproject/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from registration.backends.simple.views import RegistrationView
# 创建一个新类,用来在用户登录成功时引导他回首页
class MyRegistrationView(RegistrationView):
def get_success_url(selfself,request, user):
return '/rango/'
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')),
url(r'^accounts/register/$', MyRegistrationView.as_view(), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
27.美化Django
在这一节,我们将讲解如何用Twitter Bootstrap toolkit来美化Django,我们不会讲Bootstrap背后的工作原理,并且,在讲解时我们会假定你对CSS有一定的了解。如果你从来没接触过CSS,甚至连CSS是什么都不知道,赶紧找度娘去恶补一下CSS的基础知识吧。
要学会使用Bootstrap,你可以访问Bootstrap的官方网站(),那里有一些例子可以学习。从这个网站可以看一些布局的范例()。比如我们参考下面这个范例来设计Rango的样式()。
我们可以直接将上面这个dashboard的页面源代码拿过来修改成base.html,具体改动的内容如下:
将&../../&替换为“
将&dashboard.css&替换为“
移除顶部的搜索表单;
将所有不必要的内容删除,用 “{% block body_block %}{% endblock %} ”来代替;
修改title元素为:&title&Rango - {% block title %}实战Django!{% endblock %}&/title&
修改项目名称为Rango,改这句:&a class=&navbar-brand& href=&/rango/&&Rango
在顶部导航栏中添加首页、登录、注册等链接;
添加一个侧边栏区块,例如:{% block side_block %}{% endblock %}
改好后的base.html会是下面这个样子:
templates/base.html:
&!DOCTYPE html&
&html lang=&en&&
&meta charset=&utf-8&&
&meta http-equiv=&X-UA-Compatible& content=&IE=edge&&
&meta name=&viewport& content=& initial-scale=1&&
&meta name=&description& content=&&&
&meta name=&author& content=&&&
&link rel=&icon& href=&/favicon.ico&&
&title&Rango - {% block title %}实战Django!{% endblock %}&/title&
&link href=&/dist/css/bootstrap.min.css& rel=&stylesheet&&
&link href=&/examples/dashboard/dashboard.css& rel=&stylesheet&&
&!--[if lt IE 9]&
&script src=\'#\'" ///html5shiv/3.7.2/html5shiv.min.js&&&/script&
&script src=\'#\'" ///respond/1.4.2/respond.min.js&&&/script&
&![endif]--&
&div class=&navbar navbar-inverse navbar-fixed-top& role=&navigation&&
&div class=&container-fluid&&
&div class=&navbar-header&&
&button type=&button& class=&navbar-toggle collapsed& data-toggle=&collapse& data-target=&.navbar-collapse&&
&span class=&sr-only&&Toggle navigation&/span&
&span class=&icon-bar&&&/span&
&span class=&icon-bar&&&/span&
&span class=&icon-bar&&&/span&
&a class=&navbar-brand& href=&/rango/&&Rango&/a&
&div class=&navbar-collapse collapse&&
&ul class=&nav navbar-nav navbar-right&&
&li&&a href=&{% url 'index' %}&&首页&/a&&/li&
{% if user.is_authenticated %}
&li&&a href=&{% url 'restricted' %}&&限制页面&/a&&/li&
&li&&a href=&{% url 'auth_logout' %}?next=/rango/&&注销&/a&&/li&
&li&&a href=&{% url 'add_category' %}&&新建分类&/a&&/li&
{% else %}
&li&&a href=&{% url 'registration_register' %}&& 注册&/a&&/li&
&li&&a href=&{% url 'auth_login' %}&&登录&/a&&/li&
{% endif %}
&li&&a href=&{% url 'about' %}&&关于&/a&&/li&
&div class=&container-fluid&&
&div class=&row&&
&div class=&col-sm-3 col-md-2 sidebar&&
{% block side_block %}{% endblock %}
&div class=&col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main&&
{% block body_block %}{% endblock %}
&!-- Bootstrap core JavaScript
================================================== --&
&!-- Placed at the end of the document so the pages load faster --&
&script src=&/ajax/libs/jquery/1.11.1/jquery.min.js&&&/script&
&script src=&/dist/js/bootstrap.min.js&&&/script&
&script src=&/assets/js/docs.min.js&&&/script&
&!-- IE10 viewport hack for Surface/desktop Windows 8 bug --&
&script src=&/assets/js/ie10-viewport-bug-workaround.js&&&/script&
有了这个基础模板,我们可以将这个新样式快速应用到其它模板中,比如我们来调整”关于“页面的模板:
templates/rango/about.html:
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}About{% endblock %}
{% block body_block %}
&div class=&page-header&&
&h1&关于Rango&/h1&
&p&欢迎访问Rango!&/p&
&p&点击这里返回&a href=&{% url 'index' %}&&首页&/a&。&/p&
&p&这是一张Rango的图片!&/p&
width=&300& src=&{% static &rango.jpg& %}& alt=&Rango& /&
{% endblock %}
接下来,让我们来搞定首页。
templates/rango/index.html:
{% extends 'base.html' %}
{% load staticfiles %}
{% block title %}Index{% endblock %}
{% block body_block %}
{% if user.is_authenticated %}
&div class=&page-header&&
&h1&{{ user.username }},欢迎来到Rango!&/h1&
{% else %}
&h1&欢迎来到Rango!&/h1&
{% endif %}
&div class=&panel panel-primary&&
&div class=&panel-heading&&
&h3 class=&panel-title&&分类&/h3&
{% if categories %}
&ul class=&list-group&&
{% for category in categories %}
&li class=&list-group-item&&&a href=&{% url 'category'
category.slug %}&&{{ category.name }}&/a&&/li&
{% endfor %}
{% else %}
&strong&目前还没有可用分类。&/strong&
{% endif %}
&div class=&panel panel-primary&&
&div class=&panel-heading&&
&h3 class=&panel-title&&页面&/h3&
{% if pages %}
&ul class=&list-group&&
{% for page in pages %}
&li class=&list-group-item&&&a href=&{{page.url}}&&{{ page.title }}&/a&&/li&
{% endfor %}
{% else %}
&strong&目前还没有可用页面。&/strong&
{% endif %}
&p& 访问数: {{ visits }}&/p&
{% endblock %}
接下来我们来搞定登录页面。在Bootstrap,他们已经有一个非常不错的登录例子(),我们可以参照这个例子来改写登录模板:
templates/registration/login.html:
{% extends &base.html& %}
{% block body_block %}
&link href=&/examples/signin/signin.css& rel=&stylesheet&&
&form class=&form-signin& role=&form& method=&post& action=&.&&
{% csrf_token %}
&h2 class=&form-signin-heading&&请登录&/h2&
&input class=&form-control& placeholder=&用户名& id=&id_username& maxlength=&254& name=&username& type=&text& required autofocus=&&/&
&input type=&password& class=&form-control& placeholder=&密码& id=&id_password& name=&password& type=&password& required /&
&button class=&btn btn-lg btn-primary btn-block& type=&submit& value=&Submit& /&登录&/button&
{% endblock %}
注册页面同样需要美化。编辑registration_form.html,改成下面这样:
templates/registration/registration_form.html:
{% extends &base.html& %}
{% block body_block %}
&form role=&form& method=&post& action=&.&&
{% csrf_token %}
&h2 class=&form-signin-heading&&注册&/h2&
&div class=&form-group& &
&p class=&required&& &label for=&id_username&&用户名:&/label&&/p&
&p&&input class=&form-control-static& size=&28& id=&id_username& maxlength=&30& name=&username& type=&text&
placeholder=&请输入用户名&/&&/p&
&div class=&form-group&&
&p class=&required&&&label for=&id_email&&E-mail:&/label&&/p&
&p&&input class=&form-control-static& size=&28& id=&id_email& name=&email& type=&email& placeholder=&请输入邮箱& /&&/p&
&div class=&form-group&&
&p class=&required&&&label for=&id_password1&&密码:&/label&&/p&
&p&&input class=&form-control-static& size=&28& id=&id_password1& name=&password1& type=&password& placeholder=&请输入密码& /&&/p&
&div class=&form-group&&
&p class=&required&&&label for=&id_password2&&密码 (确认):&/label&&/p&
&p&&input class=&form-control-static& size=&28& id=&id_password2& name=&password2& type=&password& placeholder=&请再次输入密码& /&&/p&
&button type=&submit& class=&btn btn-default&&提交&/button&
{% endblock %}
再来看看其它的页面,比如“新建页面”模板,可以改成这样:
templates/rango/add_page.html:
{% extends 'base.html' %}
{% block title %}添加页面{% endblock %}
{% block body_block %}
{% if category %}
&form role=&form&
id=&page_form& method=&post& action=&/rango/category/{{category.slug}}/add_page/&&
&h2 class=&form-signin-heading&&添加页面到 &a href=&/rango/category/{{category.slug}}/&& {{ category.name }}&/a&&/h2&
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field.help_text }}&br/&
{{ field }}&br/&
{% endfor %}
&button class=&btn btn-primary& type=&submit& name=&submit&&创建页面&/button&
&p&此分类不存在.&/p&
{% endblock %}
我们可以用同样的方式来修改“新建分类”模板。剩下那些模板的调整,就由你自己来完成吧!
【未完待续】
本文版权归所有,欢迎转载,转载请注明作者和出处。谢谢!
了这篇文章
类别:未分类┆阅读(0)┆评论(0)It's finally ready for developer consumption, my
Django app.
With this super simple app in your Django project, you can easily apply a site-wide Bootstrap theme in a matter of minutes.
Here's an example base.html you would use in your project's templates directory:
{% extends 'bootstrap_theme/starter.html' %}
{% block title %}Bootstrap Django site{% endblock %}
{% block branding %}Django Project{% endblock %}
{% block content %}
Yes Folks, this is how easy it is to implement bootstrap in Django now.
Be sure to look at the other templates available to see what &b&blocks&/b& you can override.
{% endblock %}
Be sure to add bootstrap_theme to your INSTALLED_APPS, and this will work like a charm.
Here is a list of available base templates you can extend:
This is the main template which the ones below extend to create the Bootstrap experience.
starter.html
This is the exact starter template from the Bootstrap documentation, it's the most basic and easiest to extend.
fluid.html
Similar to starter, except it allows users to sign in and out, and has a left navigation bar.
marketing.html
The marketing template directly from the bootstrap examples page tailored to work with Django.
marketing_narrow.html
From the bootstrap example page, another version of a marketing page you can try out.
marketing_clean.html
Yet another marketing page example, also from the bootstrap examples page.
carousel.html
The bootstrap carousel example made for Django, the bonus is that the Carousel is driven from the database, so it's easy to add and edit the big Carousel banner.
Wait, there's more...
There's a large amount of templatetags at your disposal as well in the bootstrap library which you can load into any template you need it in.
Here's what's included:
buttonlink url icon
You can create bootstrap themed buttons with this tag.
emphasis filter
This filter will add special styling to the text, such as muted which is the default, or anything in the text- CSS components.
abbrev filter
This handy filter will create an abbreviation text.
yesnoicon filter
Depending if the value is True or False a different icon is displayed.
ratingicon filter
This super handy filter will create a specific amount of star icons depending on the value.
link filter
Pass in any model object which has a get_absolute_url and it will automatically make it click-able.
carousel group_name
This is used to display a database driven Carousel.
The group_name parameter is which group in the database model to display for this instance.
navbar_gradient first_color last_color
This super handy tag will automatically generate the required CSS style needed for the navbar gradient.
modal title modal_id form_action
This block tag is used to generate a modal dialog box, the last 2 options are optional.
The first is the title of the dialog box, the second is the unique div ID for the modal, and the last one is the URL which the form inside the modal will be posted to, if the user saves the changes.
This is used to display bootstrap icons easily.
modal_button title modal_id icon
This is used to generate the required A tag to display a modal dialog box generated above.
The first option is what to display on the button itself, the second is the div ID chosen, and the last one is an icon slug to place on the button.
The last two options are optional and have reasonable defaults.
Don't leave yet, there's still one last thing...
An include-able template which can display a Django form very elegantly in bootstrap!
Finally you can make your Django generated forms look great and functional with very little effort.
Here's an example contact form to get you started:
&form action=&{% url 'contact-form' %}& method=&post&&{% csrf_token %}
&fieldset&
&legend&Contact form&/legend&
{% include &bootstrap_form.html& %}
&button type=&submit& class=&btn btn-primary&&Send Message&/button&
&/fieldset&
Wow, that's a lot to take in, in the next release I will be adding a bunch of ready made templates for
so that you can get a new Django website up and running in a matter of hours.
Subscribe to:
Posted 3 years, 2 months ago by ralph lauren
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how could we communicate?
Names Kevin, hugely into UNIX technologies, not just Linux. I've dabbled with the demons, played with the Sun, and now with the Penguins.
Do you require the services of a
contractor?
Do you need both a website and hosting services?
Perhaps I can help.
This Month
If you like what you read, please consider donating to help with hosting costs, and to fund future books to review.
Powered | &
Kevin VeroneauPython学习(15)
最近在学习python当中的django框架
今天发现一个问题就是如果使用css 之类的静态资源时候不知道应该如何设置
就百度了一下方法记录下来以备后面忘记
原文章/wang_yb/archive//2024311.html
=================================================
在urls.py文件当中添加资源路径
url( r'^css/(?P&path&.*)$', 'django.views.static.serve',
&&&&&&& { 'document_root': 'D:/Users/mrwang/workspace/DjangoBlog/static/css' }
&&from&django.conf.urls&import&patterns,&include,&url&from&django.contrib&import&admin&admin.autodiscover()&&urlpatterns&=&patterns('',&include('django.contrib.admindocs.urls')),&&&&&url(r'^admin/',&include(admin.site.urls)),&&&&&url(r'^blog/',include('blog.urls')),&&&&&url(&r'^css/(?P&path&.*)$',&'django.views.static.serve',&&&&&&&&&{&'document_root':&'D:/Users/mrwang/workspace/DjangoBlog/static/css'&}&&&&&),&)&
这样就能引导css路径的到 你指定的文件夹去进行寻找css文件了
http://127.0.0.1:8000/css/xxx.css
成功返回 xxx.css当中的设定的内容 如果这样能成功那么在 网页当中进行直接的使用
/css/xxx.css应该也能够达到目的
│&&.project&│&&.pydevproject&│&&manage.py&│&├─.settings&│&&&&&&org.eclipse.core.resources.prefs&│&├─blog&│&&&&&&models.py&│&&&&&&models.pyc&│&&&&&&tests.py&│&&&&&&urls.py&│&&&&&&urls.pyc&│&&&&&&views.py&│&&&&&&views.pyc&│&&&&&&__init__.py&│&&&&&&__init__.pyc&│&├─DjangoBlog&│&&&&&&settings.py&│&&&&&&settings.pyc&│&&&&&&urls.py&&//&在这个urls.py当中指定路径&│&&&&&&urls.pyc&│&&&&&&wsgi.py&│&&&&&&wsgi.pyc&│&&&&&&__init__.py&│&&&&&&__init__.pyc&│&├─static&│&&│&&test.html&│&&│&│&&├─css&│&&│&&&&&&xxx.css&│&&│&│&&├─images&│&&├─js&│&&└─upload&└─templates&&&&&│&&base.html&&&&&│&&&&&└─blog&&&&&&&&&&&&&blog.html&
============================================本文出自 “” 博客,请务必保留此出处
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:17568次
排名:千里之外
原创:57篇
转载:31篇类别归档:django
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.
托管在SAE上的Django应用,如果使用共享型MySQL数据库服务,可以通过配置数据库路由,实现读写分离。利用SAE从库的价格优势,减少云豆开销。
SAE共享型MySQL数据库的计费策略:
新浪云SAE共享型数据库支持读写分离(Read/Write Splitting),即主数据库(Master)处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库(Slave)处理SELECT查询操作。
并且从SAE共享型MySQL的使用价格上来看:
共享型MySQL(主库)
请求次数10000次
400云豆/百万次
4元/百万次
磁盘容量20MB
5云豆/GB/天
0.05元/GB/天
共享型MySQL(从库 ...
一个搭建在SAE上的Django应用,使用新浪微博提供的Python SDK已经稳定运行一年有余,但最近开始持续出现微博认证失败的状况。
摘录微博Python SDK的错误提示如下所示:
ERROR:django.request:Internal Server Error: /weibo/auth/
Traceback (most recent call last):
File &/usr/local/sae/python/3rd/django-1.5/django/core/handlers/base.py&, line 115, in get_response
response = callback(request ...
在Django ORM中,使用order_by()方法对包含关联关系的模型进行排序时,返回结果中可能会出现重复元素。
假设有下面的两个模型Client和Interaction,Client为顾客,Interaction为交互。
Client类中包含顾客的姓名和联系方式,Interaction类中包含标题、时间、待办事项、截止日期。
class Client(models.Model):
name = models.CharField(max_length=255, unique=True)
contact = models.CharField(max_length=255, null=True, blank=True)
class Interaction(models.Model):
client = models.ForeignKey ...
有时Django开发者可能会遇到这样的需求:用户访问同一个URL,根据具体的业务需求返回不同的图片。
例如:用户访问URL /user/avatar/时,根据Session中的用户ID返回该用户的头像。
一般来说,要实现此功能可以采用下列2种方法:
使用Python图片处理模块PIL/pillow动态生成图片后直接响应返回给浏览器
使用HTTP 302,临时重定向至指定的图片URL返回给浏览器
需要注意的是,虽然HTTP 301与HTTP 302都可以实现请求的重定向,但HTTP 301为Moved Permanently,意为永久转移,该响应会被Google Chrome和Firefox等浏览器所缓存,因此不适于实现图片的动态重定向。
因而在此只能选择HTTP 302 Found 告知浏览器该响应为临时重定向,避免浏览器对重定向的结果进行缓存。
下面的代码片段实现了从3张图片中通过HTTP 302重定向随机返回一张给浏览器的功能。
import random
from django.shortcuts import ...
使用SAE部署Django应用时,如何将应用的数据库同步至线上?
在本地开发环境中,如下配置数据库,即可执行 python manage.py syncdb 直接syncdb到线上数据库。
# 线上数据库的配置
MYSQL_HOST = 'w.rdc..cn'
MYSQL_PORT = '3307'
MYSQL_USER = 'ACCESSKEY'
MYSQL_PASS = 'SECRETKEY'
= 'app_APP_NAME'
from sae._restful_mysql import monkey
monkey.patch()
DATABASES = {
'default': {
'django.db.backends.mysql ...
欢迎来到的博客
本博客基于
提供云计算资源
周一周二周三周四周五周六周日
891011121314
15161718192021
22232425262728
293031&&&&}

我要回帖

更多关于 django base.html 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信