セッションをURLに付加

セッションIDをURLの後ろに付加させるカスタムタグを試しに作ってみた.

Cookieが無効な携帯は自動でセッションIDを付加させて欲しいなぁということでurlタグならぬーmobile_urlタグを作成. 今回は,前提として「django.core.context_processors.request」を有効へ.

# settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
)

また,MIDDLEWARE_CLASSに携帯用のUserAgentMobileMiddlewareを追記.これは 「 ウノウラボ Unoh Labs: Pythonで携帯の機種判別をする (perezvon さん)」を利用しました!! いつも参考にさせていただいてます☆ありがとうございます☆

さて,ここからmobile_urlタグの作成.内容は大したことなくおおもとのurlとURLNodeをそのまま利用しただけぇ.

# project/common/templatetags/custom_tags.py

from django.conf import settings
from django.template import Library, Node
from django.template.defaulttags import URLNode
from django.utils.encoding import iri_to_uri

from uamobile.nonmobile import NonMobileUserAgent

register = Library()

class MobileURLNode(URLNode):
    def render(self, context):
        url = super(MobileURLNode, self).render(context)
        device = context['request'].device
        # 携帯以外のアクセス処理
        if isinstance(device, NonMobileUserAgent):
            return url
        # cookieが利用可能時処理
        if device.supports_cookie():
            return url
        # session_idの付加
        return url + iri_to_uri(
            '?%s=%s' % (
                settings.SESSION_COOKIE_NAME,
                context['request'].session.session_key
            )
        )

def mobile_url(parser, token):
    bits = token.contents.split(' ', 2)
    if len(bits) < 2:
        raise TemplateSyntaxError, "'%s' takes at least one argument (path to a view)" % bits[0]
    args = []
    kwargs = {}
    if len(bits) > 2:
        for arg in bits[2].split(','):
            if '=' in arg:
                k, v = arg.split('=', 1)
                k = k.strip()
                kwargs[k] = parser.compile_filter(v)
            else:
                args.append(parser.compile_filter(arg))
    return MobileURLNode(bits[1], args, kwargs)
url = register.tag(mobile_url)

てな感じ,mobile_urlなんてurlをそのままコピー.ほんでもってーMobileURLNodeもURLNodeを継承してーそのまま利用. 結局,URLNodeでrender()されたURLのケツにセッションを付加させただけ. このとき,contextからデバイス情報とセッション情報を抜き取りたかったので最初に2つの作業をやったわけです.

これで,あとはテンプレートにて {% url %} を使うのと同じように {% mobile_url %} として使えば付加してくれるんじゃないかなぁ(笑)

ん~セッションIDを付加させるのを単純に ?session_key=session_value なんてしているので問題が多そう(苦笑).ここらはもう1ヒネリとか必要になりそうです. ちょいと適当なままBLOGに載せちゃった感がありますね.まぁ僕らしい僕らしい!!突っ込みがあれば是非宜しくお願いいたします!!

[Django] [Mobile]

2007/11/25 21:36 | Comments(179)

Comments

uxNc7y <a href="http://bdpbghpdejpp.com/">bdpbghpdejpp</a>, [url=http://rvzpnabjqfqx.com/]rvzpnabjqfqx[/url], [link=http://hmasavumoovn.com/]hmasavumoovn[/link], http://dtwtpqhwaxxv.com/
2009/10/22 20:00
I support the author, sensibly laid out. , buy ultram without prescription, 8-[[[, purchase valium without a prescription, =)), meridia no prescription, 738225, buy acai berry, 4830, purchase phentermine no rx, 9937,
2009/10/25 01:42
I fully agree with the author of the article! , buy valtrex, lribry, buy nolvadex, %]]], �� �� � ��, 234, phentermine 37.5, pob, cheap finasteride 5mg, 8PP,
2009/10/25 05:09
What do you think about this issue? , order vicodin es without prescription, %DDD, order adipex without prescription, kni, buy oxycodone online, :-))), buy reductil without prescription, stpnzi, buy topamax no prescription, qik,
2009/10/24 12:51
zdrasti, buy codeine no prescription, =)), buy ritalin no prescription, qyul, buy replica rolex watches, ihzuvv, buy lasix, wxnq, buy zithromax, 740,
2009/11/01 10:44
hello, this first post, purchase phentermine without prescription, %]]], order ambien, ryzhx, purchase vicodin no prescription, 688881, buy biaxin, 0297, buy buspirone hcl online, 389,
2009/11/03 08:19
comment6, buy celexa, xxrwpq, order ritalin, xbg, order lasix online, 8-[, buy cheap maxaman, 238, buy female viagra, 25032,
2009/11/03 16:36
yPoPbz <a href="http://qfnytmugccxg.com/">qfnytmugccxg</a>, [url=http://abmvdkoblzzr.com/]abmvdkoblzzr[/url], [link=http://odspaerqtujh.com/]odspaerqtujh[/link], http://siceyykeqije.com/
2009/11/01 08:10
hi, my post, buy clomid online, %PP, buy cipro without prescription, 8), order adderall no prescription, =-((, oxycontin no prescription, 258271, purchase oxycontin, 9117,
2009/11/02 03:55
guten morgen, buy amoxicillin, 8-))), buy doxycycline, djyqty, order vicodin without prescription, 8-PP, buy phenobarbital online, =-[[[, buy rivotril 2mg no prescription, njo,
2009/11/02 12:49
comment1, order tamiflu without prescription, ovigys, cheap alprazolam no prescription, >:-DDD, buy clonazepam, >:PP, buy diazepam without prescription, rwmkx, buy klonopin 2mg no prescription, >:-PP,
2009/11/05 13:06
comment3, cheap soma no prescription, 7785, order tramadol no prescription, %]], zolpidem no prescription, 8-))), zopiclone no prescription, >:[[[, order meridia no prescription, 58032, subutramine no prescription, segutv, order ambien no prescription, 489,
2009/11/05 15:20
comment4, viagra sale, mvdvre,
2009/11/05 18:51
comment3, vicodin no prescription, 589, buy antabuse without a prescription, hds, hydrocodone no prescription, tulfd, buy cheap tylenol, daur, cheap proscar, >:OO,
2009/11/06 17:22
comment1, purchase vicodin no prescription, ovsxgb, order ritalin, 0084, acquisto cialis, rqx, buy codeine no prescription, 538, cheap hoodia diet pills, =-[[[,
2009/11/06 07:03
comment4, buy phenylpropanolamine online, 481430, order amlodipine no prescription, zda, buy cheap acyclovir, jfys, buy zithromax online, >:-OOO, buy prozac online, =-],
2009/11/07 07:09
<a href="http://hxrywdwa.com">xjhultaq</a> ntskaxhv http://rvjvtuon.com izkjndfq soasmdtu [URL=http://mdvlklfd.com]yeqsgcfr[/URL]
2009/11/10 15:25
It's a rare person who wants to hear what he doesn't want to hear.
2009/11/12 16:14
comment1, cheap vicodin, capx, buy cheap silagra, 01021, buy caverta, 8-(((, buy penegra, 93659, buy forzest online, 7345,
2009/11/13 04:12
comment4, buy synthroid without prescription, :(((, buy prednisolone without prescription, dwjc, buy aricept without prescription, >:(, buy lumigan without prescription, 351, buy accutane without prescription, 342, buy antabuse without prescription, yid, buy colchicine without prescription, :(,
2009/11/13 13:28
comment3, buy differin without prescription, 28072,
2009/11/13 20:13
[url=http://www.netvibes.com/buy-generic-viagra-online][size=32][color=#FF0000]$$$ Buy Viagra, Cialis, Levitra Online $$$[/color][/size][/url]
[url=http://www.netvibes.com/buy-generic-viagra-online][IMG]http://img49.imageshack.us/img49/9483/discountgenericonlined.jpg[/IMG]
][/url]

*THE LOWEST PRICE GUARANTEED
*Fully confidential
*No Prior Prescription Required
*Free Consultation
*Fast And Discreet Shipping Worldwide
*24/7 Customer Support
*And Much More

[url=http://www.netvibes.com/buy-generic-viagra-online][b]BUY PILLS NOW[/b][/url]

--------------------------------------------------------------

loving a man with erectile dysfunction
canadian viagra
erectile dysfunction diabetes journal
penis enlargement that solves erectile dysfunction
erectile dysfunction bent penis
sudden erectile dysfunction
xanax erectile dysfunction
vacumn pumps for erectile dysfunction
pornography and erectile dysfunction
nasal sprays and erectile dysfunction
2009/11/16 08:44
Very nice site! cheap cialis http://opeaixy.com/qsqavsa/4.html
2009/11/16 23:05
Very nice site!
2009/11/16 23:05
Is there life before death?
2009/11/12 16:14
To err is human, but to really foul things up requires a computer.
2009/11/12 20:51
comment5, viagra for sale, xioruh,
2009/11/13 21:54
Very nice site! <a href="http://opeaixy.com/qsqavsa/1.html">cheap viagra</a>
2009/11/16 23:04
Very nice site! [url=http://opeaixy.com/qsqavsa/2.html]cheap cialis[/url]
2009/11/16 23:05
Go often to the house of thy friend; for weeds soon choke up the unused path.
2009/11/17 20:06
Hello!
2009/11/19 00:14
History teaches us that men and nations behave wisely once they have exhausted all other alternatives.
2009/11/18 22:31
Hello!
2009/11/19 00:21
Hello!
2009/11/19 16:32
Hello!
2009/11/19 16:58
Hello!
2009/11/19 18:48
Very nice site! [url=http://aixopey.com/qqavst/2.html]cheap cialis[/url]
2009/11/19 19:59
comment5, viagra sale, 631265,
2009/11/19 20:53
The best of us must sometimes eat our words.
2009/11/19 21:48
comment2, buy albuterol without prescription, jshm,
2009/11/19 18:34
Very nice site! <a href="http://aixopey.com/qqavst/1.html">cheap viagra</a>
2009/11/19 19:59
Very nice site! cheap cialis http://aixopey.com/qqavst/4.html
2009/11/19 19:59
comment1, buy celestone without prescription, >:OO, buy prednisone without prescription, rib, buy dexamethasone without prescription, =-[, buy ambroxol without prescription, 849144, buy bromhexine without prescription, rquy, buy carbocisteine without prescription, vbpvhp, buy phenylpropanolamine without prescription, fwz,
2009/11/20 00:01
Hello!
2009/11/20 16:00
Hello!
2009/11/20 16:25
comment5, buy phenobarbital, gdxdbz, buy darvocet online, 810299, buy rivotril online, :D, buy finasteride, 41255, buy proscar online, %O,
2009/11/21 12:00
No great improvements in the lot of mankind are possible until a great change takes place in the fundamental constitution of their modes of thought.
2009/11/21 19:35
Hello!
2009/11/20 14:43
Hello!
2009/11/20 16:50
comment1, purchase kamagra online, qjfd, buy mebendazole sale, 8D, buy cheap reductil, %-P, order vicodin without prescription, ytacud, buy tylenol, 8-]],
2009/11/21 00:53
Computer Science is no more about computers than astronomy is about telescopes.
2009/11/21 01:37
It's the opinion of some that crops could be grown on the moon. Which raises the fear that it may not be long before we're paying somebody not to.
2009/11/21 13:05
fhqhtdut http://djnzyxxu.com iwrstqwd pvhwxjii <a href="http://fhyhambl.com">hanzdgtf</a> [URL=http://qdjzxmrd.com]zeordyjd[/URL]
2009/11/25 06:28
comment1, buy adderall online, %]], buy oxycontin no rx, dwvdf, cialis 20mg, 346761,
2009/11/25 07:01
The reason grandparents and grandchildren get along so well is that they have a common enemy.
2009/11/25 11:57
Hello!
2009/11/25 21:44
Hello!
2009/11/25 21:47
Fresh clean sheets are one of life's small joys.
2009/11/26 05:52
Hello!
2009/11/26 14:22
Hello!
2009/11/26 15:01
Hello!
2009/11/26 21:19
Hello!
2009/11/26 21:19
Hello!
2009/11/26 22:04
comment6, buy vicodin without prescription, 009, buy prozac no prescription, mxb, rivotril 2mg sale without a prescription, >:((, buy duromine, 5013, prednisone without prescription, 91527,
2009/11/27 06:45
Hello!
2009/11/27 10:08
Hello!
2009/11/27 10:37
Hello!
2009/11/27 10:49
and
Hello!
2009/11/27 12:50
Hello!
2009/11/27 14:02
Hello!
2009/11/27 14:11
Genius is one per cent inspiration, ninety-nine per cent perspiration.
2009/11/23 20:58
comment3, cheap bontril no prescription, 8913, buy oxycodone online, 947, cheap didrex no prescription, %-D, norco no prescription, 51689, synthroid no prescription, =-(((,
2009/11/25 21:20
Hello!
2009/11/26 02:40
Hello!
2009/11/26 13:58
Hello!
2009/11/26 14:53
comment2, buy percocet no prescription, kuynps, order codeine online, >:((, order percocet no prescription, 8(, buy mebendazole tablets no prescription, fwpse, buy phenobarbital no prescription, %-PPP,
2009/11/26 21:40
comment3, buy codeine, cyymln, buy naproxen, =OOO, cheap proscar, eeuyg, buy avodart without prescription, 48000, buy keflex online, hsu,
2009/11/27 09:53
Hello!
2009/11/27 10:23
Things are only impossible until they're not.
2009/11/27 11:50
A fine quotation is a diamond on the finger of a man of wit, and a pebble in the hand of a fool.
2009/11/28 04:48
5CSoiV comment2, buy vicodin without prescription, rmadiq, cheap hydrocodone no prescription, oti, buy adipex without prescription, :-))), order cheap codeine, %)), buy proscar, :)),
2009/11/28 08:53
Hello!
2009/11/28 14:16
Hello!
2009/11/28 17:01
Everything has its wonders, even darkness and silence, and I learn whatever state I am in, therin to be content
2009/11/29 04:09
Of all noises, I think music is the least disagreeable.
2009/11/29 08:21
comment1, buy ritalin, 5419, buy amoxicillin without rx, 8DD, buy zithromax without a prescription, xotigo, cheap norvasc 5mg without prescription, 724666, buy orlistat, amdusj,
2009/11/30 04:39
Hello!
2009/11/30 08:35
Hello!
2009/11/30 09:12
Hello!
2009/12/01 00:08
comment4, purchase xanax without prescription, =(((, buy valium 10mg, >:P, buy ritalin without a prescription, gceff, order vicodin without prescription, 893288, buy propecia no prescription, :O,
2009/12/01 02:37
comment4, order ritalin no rx, 107, phenobarbital, 94526, buy duromine without prescription, 6987, buy cheap codeine, bfpo, rivotril for sale without prescription, 105,
2009/12/01 06:54
Good habits, which bring our lower passions and appetites under automatic control, leave our natures free to explore the larger experiences of life.
2009/12/01 07:22
Keep true to the dreams of thy youth.
2009/12/01 07:23
It really doesn't matter if the person who hurt you deserves to be forgiven. Forgiveness is a gift you give yourself. You have things to do and you want to move on.
2009/12/01 07:23
[URL=http://knvlwwyp.com]gwhjdclp[/URL] <a href="http://ptjrpsvm.com">jwskgiih</a> zoclxjst http://iexmeugi.com xelqqvnl vtltlmjh
2009/12/01 14:23
Looks are part of business. A businessman should never stand out more than his customers. His mannerisms, his clothes, everything about him... Moderation is the key.
2009/12/01 19:54
Innovation has nothing to do with how many R&D dollars you have. When Apple came up with the Mac, IBM was spending at least 100 times more on R&D. It's not about money. It's about the people you have, how you're led, and how much you get it.
2009/12/01 19:55
Before we set our hearts too much upon anything, let us examine how happy those are who already possess it.
2009/12/01 19:55
Before we set our hearts too much upon anything, let us examine how happy those are who already possess it.
2009/12/01 19:55
Hello!
2009/11/28 15:49
Comment Form