Python Development [Django]: Composite Search, JSONP, XSS Filtering
Keywords:
Python
Django
JSON
Database
encoding
Combinatorial search
When doing background blogging, you need to do different searches according to the type of articles.
1. Simple implementation
Associated documents:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^index.html/$',views.index),
url(r'^article/(?P<article_type>\d+)-(?P<category>\d+).html/$',views.article)
]
url.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.condition a{
display:inline-block;
padding: 3px 5px;
border: 1px solid black;
}
.condition a.active{
background-color: brown;
}
</style>
</head>
<body>
<h2>Filter condition</h2>
<div class="condition">
{% if kwargs.article_type == 0 %}
<a href="/article/0-{{ kwargs.category }}.html" class="active">whole</a>
{% else %}
<a href="/article/0-{{ kwargs.category }}.html">whole</a>
{% endif %}
{% for row in article_type %}
{% if row.id == kwargs.article_type %}
<a class="active" href="/article/{{ row.id }}-{{ kwargs.category }}.html">{{ row.caption }}</a>
{% else %}
<a href="/article/{{ row.id }}-{{ kwargs.category }}.html">{{ row.caption }}</a>
{% endif %}
{% endfor %}
</div>
<div class="condition">
{% if kwargs.category == 0 %}
<a class="active" href="/article/{{ kwargs.article_type }}-0.html">whole</a>
{% else %}
<a href="/article/{{ kwargs.article_type }}-0.html">whole</a>
{% endif %}
{% for row in category %}
{% if row.id == kwargs.category %}
<a class="active" href="/article/{{ kwargs.article_type }}-{{ row.id }}.html">{{ row.caption }}</a>
{% else %}
<a href="/article/{{ kwargs.article_type }}-{{ row.id }}.html">{{ row.caption }}</a>
{% endif %}
{% endfor %}
</div>
<h2>Query results</h2>
<ul>
{% for row in articles %}
<li>{{ row.id }}-{{ row.title }}------[{{ row.article_type.caption }}]-[{{ row.category.caption }}]</li>
{% endfor %}
</ul>
</body>
</html>
article.html
Database structure:
from django.db import models
# Create your models here.
class Categoery(models.Model):
caption = models.CharField(max_length=16)
class ArticleType(models.Model):
caption = models.CharField(max_length=16)
class Article(models.Model):
title = models.CharField(max_length=32)
content = models.CharField(max_length=255)
category = models.ForeignKey(Categoery)
article_type = models.ForeignKey(ArticleType)
Processing documents:
from . import models
def article(request,*args,**kwargs):
search_dict = {}
for key,value in kwargs.items():
kwargs[key] = int(value) # Converting character types to int types facilitates front-end comparisons such as if a = b
if value !='0':
search_dict[key] = value
articles = models.Article.objects.filter(**search_dict) # A dictionary is a space-time representation of all searches
article_type = models.ArticleType.objects.all()
category = models.Categoery.objects.all()
return render(request,'article.html',{'articles':articles,
'article_type':article_type,
'category':category ,
'kwargs':kwargs})
Note: Implementing this function is not difficult, the most important thing is to clarify the ideas in it. First, we should determine the url access path format http://127.0.0.1:8000/article/0-0.html, the first 0 represents the article_type field, the second 0 represents the category field, if it is zero, it means searching all the information in this field, confirming this is the first step of success, processing the document has a search place. The second key point is to generate the dictionary search_dict for relevant search, if 0 means all search; the third key point is also a very clever way to pass the parameter kwargs to the front end again, which is absolutely magical! ___________
2. Another attempt (load memory tuning)
Because the ArticleType type is the data that the blog is destined to die, it will not change later. It can load the data into memory to speed up the query speed.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.condition a{
display:inline-block;
padding: 3px 5px;
border: 1px solid black;
}
.condition a.active{
background-color: brown;
}
</style>
</head>
<body>
<h2>Filter condition</h2>
<div class="condition">
{% if kwargs.article_type_id == 0 %}
<a href="/article/0-{{ kwargs.category_id }}.html" class="active">whole</a>
{% else %}
<a href="/article/0-{{ kwargs.category_id }}.html">whole</a>
{% endif %}
{% for row in article_type%}
{% if row.0 == kwargs.article_type_id %}
<a class="active" href="/article/{{ row.0 }}-{{ kwargs.category_id }}.html">{{ row.1 }}</a>
{% else %}
<a href="/article/{{ row.0 }}-{{ kwargs.category_id }}.html">{{ row.1 }}</a>
{% endif %}
{% endfor %}
</div>
<div class="condition">
{% if kwargs.category_id == 0 %}
<a class="active" href="/article/{{ kwargs.article_type_id }}-0.html">whole</a>
{% else %}
<a href="/article/{{ kwargs.article_type_id }}-0.html">whole</a>
{% endif %}
{% for row in category %}
{% if row.id == kwargs.category_id %}
<a class="active" href="/article/{{ kwargs.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
{% else %}
<a href="/article/{{ kwargs.article_type_id }}-{{ row.id }}.html">{{ row.caption }}</a>
{% endif %}
{% endfor %}
</div>
<h2>Query results</h2>
<ul>
{% for row in articles %}
<li>{{ row.id }}-{{ row.title }}------[{{ row.article_type }}]-[{{ row.category.caption }}]</li>
{% endfor %}
</ul>
</body>
</html>
article.html
3. Using simple_tag to optimize the code
Create the template tags directory and create the filter.py file under the directory:
1. What is homologous strategy?
Note: When you click on js to get the result directly, the browser displays the following error information. Because the browser only accepts the information sent from http://127.0.0.1:8000, the information sent from the weather website is directly blocked. This is the homology strategy. Is there any way to solve this problem?
2. Skillfully label src attributes with script
Note: js requests for convenience, or request the current program url; after executing the above code, you will find a magical situation, the page will pop up 123 information, indicating that script has succeeded in obtaining information.
3. Modify the front and back ends slightly and make the use more dynamic.
Note: When js sends a request, take the callback parameter and define the method corresponding to the parameter, the background will pass the data into this method and execute it; as for printing or cartridge, it depends on the user's own needs to deal with; the principle and implementation process of jsonp is the realization of the above code.
4. Example application + ajax
list({data:[ { "week":"Sunday", "list":[ { "time":"0030", "name":"Six episodes of the All-Night Theatre", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0530", "name":"<都市现场>60分钟精编版(重播)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0630", "name":"<快乐生活一点通>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0700", "name":"<e早晨报>60分钟直播版块", "link":"http://www.jxnt v.cn/live/jxtv2 .shtml" }, { "time":"0800", "name":"精选剧场四集连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1120", "name":"<地宝当家>(重播)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1200", "name":"<都市60分>60分钟直播版块", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "Time": "1300", "time", " 1 300","name","name","name","name","Who is WinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWinWintime"", "link"":::http:///////////www.jxntv.cn/live/jxtv2.shtml"}, {"time":::::"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""/live/jxtv2.shtml""""" "" """""""", "hthththththththththt" hththththththththththtv.cn/live/jxtv2.shtml"}, {time":"1730","name":"Treasure Home >", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1800", "name":"<都市现场>90分钟直播版块", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1930", "name":"<都市情缘>", "link":"http://www.jxntv .cn /live/jxtv2.shtml" }, { "time":"2 000", "name":"<晚间800>", "link":"http://www.jxntv.cn/live/jxtv2.sh tml" }, { "time":"2020", "name":"<都市剧场>黄金剧(第1集)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2110", "name":"<都市剧场>黄金剧(第2集)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2200" "N ame": "n ame": "<shooting a case>", "link": "http:///w w.jxntv.cn/live/jxtv2.shtml"}, {time":///www.jxntv.cn/live/jxtv2.shtml"}, {time:///www.jxntv.cn/live/jxtv2.shtml"""""""", "link":http:////www.jxntv.cn/live/jxtv2.shtml""}, {"time"""{"2250"""""""""""""""name","link""::::::::::::xtv2.shtml"},{week", "Monday", "list": [{time","0030","n ame":"Six episodes of the All-Night Theater Broadcast Continuously ", "l ink":"http://www.jxntv.cn/live/jxtv 2.shtml" }, { "time":"0530", "name":"<都市现 场>60分钟精编版(重播)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0630", "name":"<快乐生活一点通>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0700", "name":"<e早晨报>60分钟直播版块", "link":"http://www .jxntv .cn/live/jxtv2.shtml" }, { "time":"0 800", "name":"精选剧场四集 连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1120", "name":"<地宝当家>(重播)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1200", "name":"<都市60分>60分钟直播版块", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "ti me":"13 00", "name":"<谁是赢家>", "link": "http://www.jxntv .cn/live/jxtv2.shtml" }, { "time":"1400", "name":"女性剧场三集连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1700", "name":"<快乐生活一点通>精编版", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1730", "name":"<地宝当家>", "link":" http://w ww.jxntv.cn/live/jxtv2.shtml" }, { "ti me":"1800" "Name": "Name": "Urban Live 90-minute Live Blocks", "link": "http:///www.jxntv.cn/live/jxtv2.shtml"}, {time":1930","""""""""""""""name"""""""""""""""""""Urban Love"""""""""""""""""""", "link"::://www.jxntv.jxntv.cn/live/jxtv2.shtml"}, {"time"""""""""""""""""""""""""""""""""""""""Live/jxtv2 .shtml"}, {"time": "2020", "name": ""Metropolita n Theatre"Golden Drama (Episode 1) )", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2110", "name":"<都市剧场>黄金剧(第2集)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2200", "name":"<拍案>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2230", "Name": "Jiangxi News Broadcast (replay)", "link": "http:///www.jxntv.cn/live/j xtv2.shtml"}, {time":"2250", {time", {2250","""""""""""""""""","name":"evening theatre of metropolis","link":"http:///www.jxntv.cn/live/list/jxtv2.shshtml.shtml"}]]]}, {""{"""""":Tuesday",,""""""""""""""""""""""""""""""""""""""//www.jxntv.cn/live/j xtv2.shtml"}, {time": "0530", "name": "<urban present" 场>60分 钟精编版(重播)", "link":"http://www.jx ntv.cn/live/jx tv2.shtml" }, { "time":"0630", "name":"<快乐生活一点通>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0700", "name":"<e早晨报>60分钟直播版块", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0800", "name":"精选剧场四集连播", "lin TreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTreTre Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Tree Trere rererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererererer erer erererererererererererererererererererererererererererererererererererererererererererererererererererererererererejxntv.jxntv.jxntv.cn.cn/ live/jxtvvvvv.cn/live/jxtxtvvvvvvv.cn/live/jxtcn/live/jxtv2.shtml"}, {"time": "1400", "name": "three episodes of women's theatre serials" "Link": "htttp:////www.jxntv.hthtttp:////www.jxntv.jxntv.cn/live/j xtv2. shtml"}, {"time": "1700",""name": {"time"",,,""1700",,"""""""""Have a HaHaHaHaHaHaHaHaHaHaHaHaHaHaHaHahttttttp:///www.jxntntv.jxntv.cn/live/ jxtv2.cn/live/jxtv2.shshtmtml,"""""""""""""""""""""""htttp:///////www{Time": "1800", "Name": "Urban Live 90 Minutes Live Block", "Link": "http://www.jxntv" .cn/live/jxtv2.shtml" }, { "time":"1930", "nam e":"< 都市情缘>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2000", "name":"<晚间800>", "link":"http://www.jxntv.cn/live/jxtv2 .shtml" }, { "time":"2020", "name":"<都市剧场>黄金剧(第1集)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2110", "na me":"<都市剧场>黄金剧(第2集)", "link":"http://www. jxntv. cn/live/jxtv2.shtml" }, { "time":"2200", "name":"<拍案>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2230", "name":"江西新 闻联播(重播)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2250", "name":"都市晚剧场", "link":"http://ww w.jxntv.cn/live/jxtv2.shtml" } ] },{ "week":"周三", "list":[ { "ti me":"00 30", "name":"通宵剧场六集连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0530", "name":"<都市现场>60分钟精编 版(重播)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0630", "name":"<快乐生活一点通>", "link" Htttp:////www.jxntv.cn/live/jxtv2.shtml""}, {time"""time":::time""::::Time":: :"time": ::"time":::"time":::::""""time"""""""""""""""""""time"///www.jxntv.cn/live/jxtv2.jxtv2.shtml"""""""":httttp://///www.www.jxxntv.jxntv.cn/live/l ive/jxtv2.jxtv2.jxtv2.shshtml""""""""""Time", "1120", "name", "Treasure Home" (replay), "link": "http://www.jxntv.cn/live/j" xtv2.shtml" }, { "time":"1200", "name":"<都市60分>60分钟直播版 块", "lin k":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1300", "name":"<谁是赢家>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time" :"1400", "name":"女性剧场三集连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1700", "name": "Happy Living a Happy Life is a master edition", "link": "http:///www.jxntv .cn/live/jx tv2.shtml"}, {time",{time""""""""1730",""name""","Treasure Home",,"""""","link":http:///www.jxntv.cn/live/jxtv2.cn/live/jxtv2.shtml"""""""""""""" """"""""{"time"":1800","""""""""""""""""""""""""""""""""""/ jxtv2.shtml"}, {time": "1930", "name", "urban love", "lin" k":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2000", "name":"< 晚间800>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2020", "name":"<都市剧场>黄金剧(第1集)", "link":"http://www.jxntv.cn/live/jxtv 2.shtml" }, { "time":"2110", "name":"<都市剧场>黄金剧(第2集)", "link":"http://www.jxntv.cn /live/jxtv2.shtml" }, { "time":"2200", "name":"<拍案>", "link":"http://www.jxntv.cn/li ve/jxtv2.shtml " }, { "time":"2230", "name":"江西新闻联播(重播)", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"2250", "name":"都市晚剧 场", "link":"http://www.jxntv.cn/live/jxtv2.shtml" } ] },{ "week":"周四", "list":[ { "time":"00 30", "name":"通宵剧场六集连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"0530" "Name": "name": "<urban scene>60 minutes edition (replay)", "link": "http:///www.jxntv.cn/live/jxtv2.shtml"}, {"time": "0630",""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""":://www.jxntv.jxntv.cn/live/jxtv2.shtml"""""""jxntv.cn/live/jxtv2.shtml"}, {time": "0800", "name": "Select ed Theatre" 四集连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1120", "name":"<地宝当家>(重播)", "link":"http://www.jxntv.cn/live /jxtv2.shtml" }, { "time":"1200", "name":"<都市60分>60分钟直播版 块", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1300", "name":"<谁是赢家>", "link":"http://www.j xntv.cn/live /jxtv2.shtml" }, { "time":"1400", "name":"女性剧场三集连播", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1700", "name":"<快 乐生活一点通>精编版", "link":"http://www.jxntv.cn/live/j xtv2.shtml" }, { "time":"1730", "name":"<地宝当家>", "link":"http://www.jxntv.cn/live/jxtv2.shtml" }, { "time":"1800", "Name": "<Urban Live>90-minute Live Blocks",""link":http:///www.jxntv.cn/live/jxtv2.shtml"},{time""1930",""""""""name""""""""""""name": "<urban romance",,"""link":http://www.jxntv.cn/live/jxtv2.cn/live/jxtv2.shtml"}, {time""""""""""""""2000"""""""""""""""""""""<evening 800name>""""""""""""""/jxtv2.shtml"}, {time": "2020", "name": "<City Theatre > Golden Drama (Episode 1)," "link": "http" ://www.jxntv.c n/live/jxtv2.shtml"}, {time":"2110",""name": <metropolitan theatre> golden drama (episode 2)," "link":"http://www.jxntv.cn/live/jxtv2.shtml"}
Data Obtained by Website
Note: The process of implementation is the same as the code written by the 3rd person. It is also to create a script and then delete it.