将jquery cookie写入sqlite |(write jquery cookie to sqlite | localhost django)

将此jquery.cookie写入SQLite数据库的最佳方法是什么? (当然没有形式)

$ .cookie('cart')=

"[{"name":"Cinnamon bun","image":"/static/images/items/bakery/cinnamon_bun.jpg", "price":" 5 uah","quantity":"2","alias":"cinnamon"}, {"name":"Croissant","image":"/static/images/items/bakery/croissant.jpg", "price":" 10 uah","quantity":"1","alias":"croissant"}, {"name":"Donut","image":"/static/images/items/bakery/donuts.jpg", "price":" 7 uah","quantity":"4","alias":"donut"}, {"name":"Chocolate chip cookies","image":"/static/images/items/bakery/cookies.gif", "price":" 30 uah","quantity":"2","alias":"cookies"}]"

What is the best way to write this jquery.cookie to SQLite database? (without form of course)

$.cookie('cart') =

"[{"name":"Cinnamon bun","image":"/static/images/items/bakery/cinnamon_bun.jpg", "price":" 5 uah","quantity":"2","alias":"cinnamon"}, {"name":"Croissant","image":"/static/images/items/bakery/croissant.jpg", "price":" 10 uah","quantity":"1","alias":"croissant"}, {"name":"Donut","image":"/static/images/items/bakery/donuts.jpg", "price":" 7 uah","quantity":"4","alias":"donut"}, {"name":"Chocolate chip cookies","image":"/static/images/items/bakery/cookies.gif", "price":" 30 uah","quantity":"2","alias":"cookies"}]"

最满意答案

它们有两种可能性:

使用simple_tag(自定义模板标签)

多亏了它,您不必担心URL用户将访问哪个。 下面写的代码将在每个请求中运行。

Docs HERE 。 (注意:它适用于1.8版本,如果您使用的是不同版本的Django,请不要忘记切换文档)。

您的代码应如下所示:(在templatetags /文件夹中的python文件中)

@register.simple_tag(takes_context=True) def your_name_of_this_function(context): """ What is this tag about? (your comment) """ request = context["request"] # Get cookies cookies = request.cookies["cart"] # Your codes for writing new lines into your models into your SQlite DB # if they are not in DB (the cookies) ...

更多关于此处的请求。 注意:cookie采用字典形式

然后将此行添加到顶部的“base.html”:

{% load your_name_of_your_tag_script_without_dot_py %}

和某处(仍然是“base.html”)这个:(例如)

{% name_of_your_function_from_that_script %}

正如我所说,这段代码将在每个请求中运行simple_tag。

在views.py中的1个函数中没有simple_tag

在这里,用户的另一种方式是必须点击特定的URL才能将cookie写入数据库。

代码:

def your_function(request): """ blablabla """ cookies = request.cookies["cart"] # Codes for creating a new line into your models ...

They are 2 possibilities:

using a simple_tag (custom template tags)

Thanks to it you don't have to worry which a URL user will visit. Codes written below will be run every single request.

Docs HERE. (Note: it's for the version 1.8, if you are using a different version of Django, don't forget switch the docs).

Your codes should look like this: (in a python file inside templatetags/ folder)

@register.simple_tag(takes_context=True) def your_name_of_this_function(context): """ What is this tag about? (your comment) """ request = context["request"] # Get cookies cookies = request.cookies["cart"] # Your codes for writing new lines into your models into your SQlite DB # if they are not in DB (the cookies) ...

More about requests HERE. Note: cookies are in the form of dictionary

Then add this line to your "base.html" at the top:

{% load your_name_of_your_tag_script_without_dot_py %}

and somewhere (still "base.html") this: (e. g. )

{% name_of_your_function_from_that_script %}

This code will run the simple_tag every single request as I said.

without simple_tag within 1 function in your views.py

Here the other way around a user have to hit a specific URL in order to write cookies into your database.

Codes:

def your_function(request): """ blablabla """ cookies = request.cookies["cart"] # Codes for creating a new line into your models ...

更多推荐