Just ignore past messages. I got successful posting of data from unity to
django localhost server. Now I am creating POST request from unity thats why
I am doing request.POST if condition.
I could see that through print statements inside this code
if request.POST:
name = request.POST['name']
print name
score = request.POST['score']
print score
hash = request.POST['hash']
print hash
before there was no output. So now I am sure that I am getting values.
Can anybody help in storing name and score into database?
i have models setup as follows.
from django.db import models
# Create your models here.
class Scores(models.Model):
name = models.CharField(max_length=100)
score = models.IntegerField()
class Meta:
verbose_name_plural ="Scores"
Thanks a ton!
Dhruv Adhia
http://thirdimension.com
On Thu, Oct 22, 2009 at 2:54 PM, Dhruv Adhia <[email protected]> wrote:
> here is the version I thought should have worked, but its not quite there
>
> def add_score(request):
> #response_dict ={}
> secret_key = "asdf789as7df89asdf87ds89a8f7sfd8"
> name =""
> score=0
> hash=""
>
> # getting the posted data
> if request.GET:
> name = request.POST['name']
> print name
> score = request.POST['score']
> print score
> hash = request.POST['hash']
> print hash
> #leaderboard = {'name': name, 'score': score, 'hash': hash}
>
> #hashlib stuff for encryption
> m = hashlib.md5()
> m.update(name+str(score)+secret_key)
> real_hash = m.hexdigest()
>
> #storing it into database
> if real_hash == hash:
> # store the name and score into the database
> leaderboard = Scores(name = request.POST['name'] ,score =
> request.POST['score'])
> leaderboard.save
>
> leaderboard = Scores.objects.all()
> return render_to_response('add_score.html', {'leaderboard':
> leaderboard})
>
>
> This version does not do what i wanted it do. which is 1) Read the posted
> data 2) Store it into database
>
> Any help would be appreciated.
>
> Thanks
>
> Dhruv Adhia
> http://thirdimension.com
>
>
>
> On Thu, Oct 22, 2009 at 1:05 PM, Dhruv Adhia <[email protected]> wrote:
>
>> Allright, I get that part.
>>
>> the url is coming from unity
>>
>> javascript part inside unity
>>
>> the part in bold is doing the post data.
>>
>> private var secretKey="mySecretKey";
>> function postScore(name, score) {
>> //This connects to a server side php script that will add the name and
>> score to a MySQL DB.
>> // Supply it with a string representing the players name and the
>> players score.
>> var hash=Md5.Md5Sum(name +""+ score + ""+ secretKey);
>>
>> *var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) +
>> "&score=" + score + "&hash=" + hash;
>>
>> // Post the URL to the site and create a download object to get the
>> result.
>> hs_post = WWW(highscore_url);*
>> yield hs_post; // Wait until the download is done
>> if(hs_post.error) {
>> print("There was an error posting the high score: " +
>> hs_post.error);
>> }
>> }
>>
>>
>> <?php
>>
>> $db = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
>> or die <http://www.perldoc.com/perl5.6/pod/func/die.html>('Could not
>> connect: ' . mysql_error());
>> mysql_select_db('my_database') or
>> die<http://www.perldoc.com/perl5.6/pod/func/die.html>
>> ('Could not select database');
>>
>> // Strings must be escaped to prevent SQL injection attack.
>> $name = mysql_real_escape_string($_GET['name'], $db);
>> $score = mysql_real_escape_string($_GET['score'], $db);
>> $hash = $_GET['hash'];
>>
>> $secretKey="mySecretKey"; # Change this value to match the value
>> stored in the client javascript below
>>
>> $real_hash = md5($name . $score . $secretKey);
>> if($real_hash == $hash) {
>> // Send variables for the MySQL database class.
>> $query = "insert into scores values (NULL, '$name',
>> '$score');";
>> $result = mysql_query($query) or
>> die<http://www.perldoc.com/perl5.6/pod/func/die.html>
>> ('Query failed: ' . mysql_error());
>> }
>> ?>
>>
>> This is the php side of things that I found online. Basically I am trying
>> to convert that to python code as you can see.
>>
>> I wrote the getting part of the data and the rest is what I am working on
>> right now.
>>
>> It would be nice,
>>
>> if I could read values sent by unity(which is fine) and store them into
>> database rather than just displaying.
>>
>> Thanks Karen.
>>
>> Dhruv Adhia
>> http://thirdimension.com
>>
>>
>>
>> On Thu, Oct 22, 2009 at 12:46 PM, Karen Tracey <[email protected]>wrote:
>>
>>> On Thu, Oct 22, 2009 at 3:25 PM, Dhruv Adhia <[email protected]> wrote:
>>>
>>>> Yep and sorry I am bit new to this stuff, I mistook 69 for 200. This
>>>> explained it
>>>> http://cwiki.apache.org/GMOxSAMPLES/using-asynchronous-http-client.data/s200.log
>>>>
>>>> so for '?' then should my url pattern for add_Score look like this
>>>> (r'^add_score/?', 'carbon_chaos.highscore.views.add_score'),
>>>>
>>>>
>>>>
>>> No, query strings don't play any part in the url matching. You might
>>> want to add a $ to the end of your existing pattern, to indicate that the
>>> inbound URL path must end after the slash after add_score...as it is your
>>> requested URL is only matching because that $ is missing from your pattern,
>>> allowing any URL path that starts with add_score/ to get routed to your
>>> add_score view, even if there is more after the add_score/ element of the
>>> URL path.
>>>
>>> What you need to do to be able to access the query string values from the
>>> request.GET dictionary is change whatever is generating that URL to generate
>>> it properly, with the ? separating the URL path from the query string. But
>>> near as I can tell you have not said anything about where that URL is coming
>>> from, so I don't know what to tell you tot fix. You need the request coming
>>> in to the server to have the ? in its proper place, you do not need the ? in
>>> the pattern.
>>>
>>> Karen
>>>
>>> >>>
>>>
>>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---