私はこれを何度も試みたが誰も助けてくれることを願っています。私はこの状況にそれを適応させるためにジャンゴの本でタンゴを使用しようとし、多くの他の例をオンラインで運営したが運がなかった。とにかく...Django - データベースにアクセスするためにjQuery/AJAXを使用する
私は現在、ユーザーにニュース記事(タイトル、説明、写真)を表示するミニゲームを持つDjangoの小さな「偽のニュース」ウェブサイトを設計しています。ファクト(Fact) 'または'フィクション(Fiction) 'を選択すると、それらが正しいかどうか、そして以前にその回答に投票した人の数が表示されます。
私はこのようなjavascriptの変数に格納されているすべてのデータがあれば、それは完全に正常に動作します:
var questions = [
["Pope Francis Shocks World, Endorses Donald Trump for President, Releases Statement",
"News outlets around the world are reporting on the news that Pope Francis has made the unprecedented decision to endorse a US presidential candidate. His statement in support of Donald Trump was released from the Vatican this evening: 'I have been hesitant to offer any kind of support for either candidate in the US presidential election but I now feel that to not voice my concern would be a dereliction of my duty as the Holy See...",
"fake", 7, 15, '../../static/images/popetrump.jpg'],
["Trump Offering Free One-Way Tickets to Africa & Mexico for Those Who Wanna Leave America",
"President elect Donald Trump has sensationally offered free one-way tickets to Mexico or Africa for anyone who wants to leave America in the aftermath of his election victory. The extraordinary, and highly controversial, offer was revealed by an aid at a press conference in New York this morning...",
"fake", 13, 17, '../../static/images/TrumpTickets.jpg'],
["Feral pig drinks 18 cans of beer, fights cow and then passes out drunk under tree",
"The swine drank 18 beers on its bender in Port Hedland, Western Australia, according to ABC News. The alcohol also made the pig hungry and was seen looking through rubbish bags for something to eat...",
"true", 8, 19, '../../static/images/pig.jpg'],
["Cinnamon Roll Can Explodes Inside Man's Butt During Shoplifting Incident",
"Las Vegas – Martin Klein, 41 of Las Vegas, was arrested after a shopping lifting incident turned horribly wrong. According to reports, Mr. Klein and his partner, Jerry Weis, had stolen several grocery items from the Las Vegas Walmart...",
"fake", 19, 7, '../../static/images/CinnamonRollMan.jpg'],
["Poll: 38% of Florida voters believe Ted Cruz could be the Zodiac Killer",
"While a 62 percent majority of voters answered 'No' when asked if they believed Cruz was responsible for the string of murders in the early 70s, 10 percent answered 'Yes' and an additional 28 percent said they were unsure...",
"true", 11, 6, '../../static/images/FloridaPoll.jpg'],
["Hamster resurrection: Pet rises from the grave at Easter after being buried in garden",
"Tink the hamster was found ‘cold and lifeless’ in the bottom of her cage and laid to rest by a couple who were looking after her for a friend. But the next day the rodent – who was not dead but hibernating – reappeared as perplexed Les Kilbourne-Smith crushed a pile of old boxes for recycling...",
"true", 15, 9, '../../static/images/hamster.jpg'],
]
私は、データベースへのすべての記事を追加することができたが、しかし私は、抽出する方法がわかりませんよ私が必要とするデータベースからのデータと、より多くの人々がゲームをプレイするにつれて、事実とフィクションの投票数を更新します。オンラインで読むことから、私はAJAXを使ってこれを行う必要があると信じていますが、次のボタンを押すたびに新しい記事を表示したり、投票数を更新したりするためにどこから始めるべきか分かりません。
うまくいけば誰かが少なくとも私を正しい方向に向けることができます。そして、これまで私が持っていたすべてのコードを投稿し、以下に関連すると思います。
game.js:
var questions = [
["Pope Francis Shocks World, Endorses Donald Trump for President, Releases Statement",
"News outlets around the world are reporting on the news that Pope Francis has made the unprecedented decision to endorse a US presidential candidate. His statement in support of Donald Trump was released from the Vatican this evening: 'I have been hesitant to offer any kind of support for either candidate in the US presidential election but I now feel that to not voice my concern would be a dereliction of my duty as the Holy See...",
"fake", 7, 15, '../../static/images/popetrump.jpg'],
["Trump Offering Free One-Way Tickets to Africa & Mexico for Those Who Wanna Leave America",
"President elect Donald Trump has sensationally offered free one-way tickets to Mexico or Africa for anyone who wants to leave America in the aftermath of his election victory. The extraordinary, and highly controversial, offer was revealed by an aid at a press conference in New York this morning...",
"fake", 13, 17, '../../static/images/TrumpTickets.jpg'],
["Feral pig drinks 18 cans of beer, fights cow and then passes out drunk under tree",
"The swine drank 18 beers on its bender in Port Hedland, Western Australia, according to ABC News. The alcohol also made the pig hungry and was seen looking through rubbish bags for something to eat...",
"true", 8, 19, '../../static/images/pig.jpg'],
["Cinnamon Roll Can Explodes Inside Man's Butt During Shoplifting Incident",
"Las Vegas – Martin Klein, 41 of Las Vegas, was arrested after a shopping lifting incident turned horribly wrong. According to reports, Mr. Klein and his partner, Jerry Weis, had stolen several grocery items from the Las Vegas Walmart...",
"fake", 19, 7, '../../static/images/CinnamonRollMan.jpg'],
["Poll: 38% of Florida voters believe Ted Cruz could be the Zodiac Killer",
"While a 62 percent majority of voters answered 'No' when asked if they believed Cruz was responsible for the string of murders in the early 70s, 10 percent answered 'Yes' and an additional 28 percent said they were unsure...",
"true", 11, 6, '../../static/images/FloridaPoll.jpg'],
["Hamster resurrection: Pet rises from the grave at Easter after being buried in garden",
"Tink the hamster was found ‘cold and lifeless’ in the bottom of her cage and laid to rest by a couple who were looking after her for a friend. But the next day the rodent – who was not dead but hibernating – reappeared as perplexed Les Kilbourne-Smith crushed a pile of old boxes for recycling...",
"true", 15, 9, '../../static/images/hamster.jpg'],
]
var questionCount = 0;
var correctAnswer = 0;
$(document).ready(function() {
$("#answer").hide();
$("#game_home").hide();
$("#game_title").html(questions[questionCount][0]);
$("#game_description").html(questions[questionCount][1]);
$("#game_picture").attr('src', questions[questionCount][5]);
$("#game_fact").click(function() {
$("#game_picture").hide();
$("#game_fact").prop('disabled', true);
$("#game_fiction").prop('disabled', true);
if(questions[questionCount][2] == "true"){
$("#your_answer").html("Correct! This story is a Fact!");
$("#current_answer_fact").html("Fact: " + (((questions[questionCount][3])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
$("#current_answer_fiction").html("Fiction: " + (((questions[questionCount][4])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
questions[questionCount][3] = questions[questionCount][3]+1;
correctAnswer += 1;
}
else{
$("#your_answer").html("Incorrect! This story is actually Fiction!");
$("#current_answer_fact").html("Fact: " + (((questions[questionCount][3])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
$("#current_answer_fiction").html("Fiction: " + (((questions[questionCount][4])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
questions[questionCount][3] = questions[questionCount][3]+1
}
$("#answer").show();
});
$("#game_fiction").click(function() {
$("#game_picture").hide();
$("#game_fact").prop('disabled', true);
$("#game_fiction").prop('disabled', true);
if(questions[questionCount][2] == "fake"){
$("#your_answer").html("Correct! This story is Fiction!");
$("#current_answer_fact").html("Fact: " + (((questions[questionCount][3])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
$("#current_answer_fiction").html("Fiction: " + (((questions[questionCount][4])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
questions[questionCount][3] = questions[questionCount][4]+1
correctAnswer += 1;
}
else{
$("#your_answer").html("Incorrect! This story is actually a Fact!");
$("#current_answer_fact").html("Fact: " + (((questions[questionCount][3])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
$("#current_answer_fiction").html("Fiction: " + (((questions[questionCount][4])/(questions[questionCount][3]+questions[questionCount][4]))*100).toFixed(2) + "%");
questions[questionCount][3] = questions[questionCount][4]+1
}
$("#answer").show();
});
$("#game_next").click(function() {
questionCount += 1;
if(questionCount == questions.length){
$("#your_answer").html("Thanks for playing!");
$("#current_answer").html("You scored " + correctAnswer + " out of " + questions.length);
$("#game_home").show();
$("#game_fact").hide();
$("#game_fiction").hide();
$("#game_title").hide();
$("#game_description").hide();
$("#game_next").hide();
$("#current_answer_fact").hide();
$("#current_answer_fiction").hide();
$("#game_or").hide();
}
else{
$("#game_picture").attr('src', questions[questionCount][5]);
$("#answer").hide();
$("#game_picture").show();
$("#game_title").html(questions[questionCount][0]);
$("#game_description").html(questions[questionCount][1]);
$("#game_fact").prop('disabled', false);
$("#game_fiction").prop('disabled', false);
}
});
});
人口スクリプト:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fact_or_fiction.settings')
import django
django.setup()
from factorfiction.models import Page, GameArticle
def populate():
game_articles = [
{"title":"Pope Francis Shocks World, Endorses Donald Trump for President, Releases Statement",
"description":"News outlets around the world are reporting on the news that Pope Francis has made the unprecedented decision to endorse a US presidential candidate. His statement in support of Donald Trump was released from the Vatican this evening: 'I have been hesitant to offer any kind of support for either candidate in the US presidential election but I now feel that to not voice my concern would be a dereliction of my duty as the Holy See...",
"answer":"fake",
"fact": 7,
"fiction": 15,
"picture": "../../static/images/popetrump.jpg"},
{"title":"Trump Offering Free One-Way Tickets to Africa & Mexico for Those Who Wanna Leave America",
"description":"President elect Donald Trump has sensationally offered free one-way tickets to Mexico or Africa for anyone who wants to leave America in the aftermath of his election victory. The extraordinary, and highly controversial, offer was revealed by an aid at a press conference in New York this morning...",
"answer":"fake",
"fact": 13,
"fiction": 17,
"picture": "../../static/images/TrumpTickets.jpg"},
{"title":"Feral pig drinks 18 cans of beer, fights cow and then passes out drunk under tree",
"description":"The swine drank 18 beers on its bender in Port Hedland, Western Australia, according to ABC News. The alcohol also made the pig hungry and was seen looking through rubbish bags for something to eat...",
"answer":"true",
"fact": 8,
"fiction": 19,
"picture": "../../static/images/pig.jpg"},
{"title":"Cinnamon Roll Can Explodes Inside Man's Butt During Shoplifting Incident",
"description":"Las Vegas – Martin Klein, 41 of Las Vegas, was arrested after a shopping lifting incident turned horribly wrong. According to reports, Mr. Klein and his partner, Jerry Weis, had stolen several grocery items from the Las Vegas Walmart...",
"answer":"fake",
"fact": 19,
"fiction": 7,
"picture": "../../static/images/CinnamonRollMan.jpg"},
{"title":"Poll: 38% of Florida voters believe Ted Cruz could be the Zodiac Killer",
"description":"While a 62 percent majority of voters answered 'No' when asked if they believed Cruz was responsible for the string of murders in the early 70s, 10 percent answered 'Yes' and an additional 28 percent said they were unsure...",
"answer":"true",
"fact": 11,
"fiction": 6,
"picture": "../../static/images/FloridaPoll.jpg"},
{"title":"Hamster resurrection: Pet rises from the grave at Easter after being buried in garden",
"description":"Tink the hamster was found ‘cold and lifeless’ in the bottom of her cage and laid to rest by a couple who were looking after her for a friend. But the next day the rodent – who was not dead but hibernating – reappeared as perplexed Les Kilbourne-Smith crushed a pile of old boxes for recycling...",
"answer":"true",
"fact": 15,
"fiction": 9,
"picture": "../../static/images/hamster.jpg"}]
for eachArticle in game_articles:
add_game_article(eachArticle["title"], eachArticle["description"], eachArticle["picture"], eachArticle["answer"], eachArticle["fact"], eachArticle["fiction"])
def add_game_article(title,description,picture, answer, fact=0, fiction=0):
games = GameArticle.objects.get_or_create(title=title)[0]
games.description=description
games.answer=answer
games.fact=fact
games.fiction=fiction
games.picture=picture
games.save()
return games
if __name__ == '__main__':
print("Starting FactOrFiction population script...")
populate()
fofgame.html:
{% extends 'factorfiction/base.html' %}
{% load staticfiles %}
{% block title_block %}
FoF Game
{% endblock %}
{% block main_body_block %}
<div class="current_article">
<div class="row">
<h1 id="game_title"></h1>
</div>
<div class="row">
<p id="game_description">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean
commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus
et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis,
ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.</p>
</div>
<button id="game_fact" type="button">Fact</button>
<p id="game_or">OR</p>
<button id="game_fiction" type="button">Fiction</button>
<img id="game_picture" src='{% static "images/fakenews.jpg" %}' alt="Article Picture" />
<div id="answer">
<h1 id="your_answer"></h1>
<p id="current_answer">Currently, other users have said:</p>
<p id="current_answer_fact"></p>
<p id="current_answer_fiction"></p>
<button id="game_next" type="button">Next</button>
<a href="{% url 'index' %}"><button id="game_home" type="button">Homepage</button></a>
</div>
</div>
{% endblock %}