【问题标题】:Python Django- get or post?Python Django-获取还是发布?
【发布时间】:2017-03-24 15:49:20
【问题描述】:

我有一个 Django 项目,在其中一个网页上,我想为一个项目收集一些信息。

其中一个字段有一个下拉列表,用户可以从中选择参与项目的团队成员的姓名。这个下拉列表有许多预定义的选项,还提供了选择“其他”的选项。

如果用户选择“其他”,“下拉列表”字段将变为“文本框”,用户可以输入他们想要选择的人的姓名。当他们开始输入时,将根据他们迄今为止输入的字母查询数据库,并在文本框下方显示可用选项列表(与输入的字母匹配)。例如,如果用户输入了“D”,将显示的可用选项列表可能包括:“Dan”、“Dave”、“Debbie”,但如果他们输入了“Da”,则可用选项列表将显示的内容仅包括:“Dan”和“Dave”。

当用户首次在项目中加载此页面时,“名称”字段为空。当我开始在该字段中输入并从显示的可用选项列表中选择一个选项时,浏览器控制台会向我显示一条错误消息:

jquery-2.2.2.min.js:4 POST http://localhost:8000/projects/5915/submit_3_5_ajax/ 500 (Internal Server Error)

我认为这可能意味着我使用了错误的方法,但我尝试同时使用 GetPost,它们都在浏览器控制台中给出了相同的错误...

python 控制台也显示这个错误:

ValueError: invalid literal for int() with base 10: ''
[10/Nov/2016 14:05:38] "POST /projects/5915/submit_3_5_ajax/ HTTP/1.1" 500 21537

据我所知,异常发生在我的forms.py 文件中的表单类内部:

class InfoForm(ValidatedForm):
    ...
    def __init__(self, *args, **kwargs):
        ...
        try:
            who_will_organise = project.assigned.get(role=Role.O).employee.first_name + project.assigned.get(role=Role.O).employee.surname[0] # I just want to get the first character of the surname here...
        except ObjectDoesNotExist: who_will_organise = None

        ...

    def save(self, commit=True):
        ...
        if data['who_will_organise']:
            ...
        else:
            # The print statements I'm seeing in the console would indicate that this is where the exception is being thrown...
            who_will_organise = Employee.objects.get(id=data['who_will_organise'])
            try:
                ...
            except ...
            ...
        ...
        return ...

页面的网址是:

url(r'^(?P<project_id>\d+)/survey/$', views.survey, name='survey'),

它调用的view 定义为:

def survey(request, project_id):
    project = Project.objects.get(id=project_id)
    survey = Survey.objects.get_or_create(project=project)[0]

    context = {
        'project': project,
        'survey_form': SurveyInformationForm(instance=survey),
    }
    return render(request, 'projects/survey.html', context)

我的浏览器控制台显示以下错误消息:

POST http://localhost:8000/projects/5915/submit_3_5_ajax/ 500 (Internal Server Error)

autosave_form.js:122 FAIL: (){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i…

FAIL: (){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i…

在我运行 Python 服务器的命令行的 Python 控制台中,我收到错误消息:

OperationalError: (1054, "Unknown column 'costing_addomit.group_id' in 'field list'")
[10/Nov/2016 15:01:24] "GET /projects/pipeline/ HTTP/1.1" 500 337434

谁能指出我在这里做错了什么?

【问题讨论】:

  • 与您显示的细节不同;不清楚为什么您认为这与 get vs post 有关。您需要在浏览器开发工具网络选项卡中显示您的 urls.py、视图和完整错误。
  • 道歉 - 我已添加 URL 并查看问题。
  • 您发帖的网址中不包含“.../survey/...”...
  • 是的,我发布到的 URL 确实包含 /survey/ - 它在 return render 行中...我在 Sublime 中获得该视图的方法是右键单击 URL 并单击'转到定义'...在我的浏览器中显示我遇到问题的页面的 URL 是:http://localhost:8000/projects/5915/survey/
  • 不,我们需要查看对 projects/5915/submit_3_5_ajax/ 的 Ajax 请求的视图和 URL。而且,正如我所提到的,来自浏览器开发工具的实际错误。

标签: jquery python django post get


【解决方案1】:

看起来您在某处将 String 转换为 Int 。

不久前我也发生过类似的事情。堆栈跟踪和/或打印内容是您最好的朋友。 我最好的猜测是上面的代码有效,问题实际上出在其他地方。

检查您在何处转换为 int,或期望 null 不为 null。问题可能就在那里。

【讨论】:

  • python 控制台显示ValueError: File "/.../site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value return int(value) ValueError: invalid literal for int() with base 10: '' [10/Nov/2016 15:56:04] "POST /projects/5915/submit_3_5_ajax/ HTTP/1.1" 500 21537 我猜这可能是你在说什么?我在我一直在查看的任何代码(urls.py、views.py 和 forms.py)中都找不到行 return int(value),也无法在任何 ajax.js 中找到它我刚刚找到的文件...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 2011-05-05
相关资源
最近更新 更多