我为移动应用程序制作了一个简单的python3 django服务器。 但是在views.py中,我在测试时遇到了错误。
这是功能之一。
from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from bns_info.models import Character, Dungeon, Tactics, Team
import json
import hashlib
import time
def login(request):
if request.method == 'POST':
data = request.body.decode("utf-8")
receivedData = json.loads(data)
receivedName = receivedData['characterName']
character = Character.objects.get(name=receivedName)
team = Team.objects.filter(teamNum=character['teamNum'])
teamDungeonType = Dungeon.objects.filter(dType=team['dType'])
retValue = {character['teamNum']:teamDungeonType['dType']}
return JsonResponse(retValue, safe=False)
else:
return HttpResponse('Request is not POST method.')
这是我的模型设计。
class Character(models.Model):
name = models.TextField(blank=True, null=True)
teamNum = models.IntegerField(db_column='teamNum', blank=True, null=True) # Field name made lowercase.
id = models.IntegerField(blank=True, primary_key=True)
class Meta:
db_table = 'Character'
我收到的错误很少,这就是消息。
Internal Server Error: /login/
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Dalek_Sec/BnS_Info/Server/appServer/bns_info/views.py", line 18, in login
character = Character.objects.filter(name=receivedName)
NameError: name 'Character' is not defined
[04/Jun/2017 21:11:54] "POST /login/ HTTP/1.1" 500 60733
我认为这足以让我定义'字符'在模型中但django说Character没有定义。问题是什么???
答案 0 :(得分:0)
像这样编辑您的视图,
def login(request):
if request.method == 'POST':
data = request.body.decode("utf-8")
receivedData = json.loads(data)
receivedName = receivedData['characterName']
character = Character.objects.get(name=receivedName)
team = Team.objects.get(teamNum=character.teamNum)
teamDungeonType = Dungeon.objects.get(dType=team.dType)
retValue = {character.teamNum : teamDungeonType.dType}
return JsonResponse(retValue, safe=False)
else:
return HttpResponse('Request is not POST method.')