django - 在 django : getting "global name context is not defined" error 中上传图片

标签 django image

我正在尝试使用 django 上传图像并遵循网站上的教程。在 views.py 我有:

def picture_upload(request):
    """
    form to upload an image together with a caption.
    saves it as a Picture in the database on POST.
    shows the last uploaded picture and let's you upload another.
    """
    picture = None
    if request.method != 'POST':
        form = PictureUploadForm()
    else:
        form = PictureUploadForm(request.POST, request.FILES)
        if form.is_valid():
            # an UploadedFile object
            uploadedImage = form.cleaned_data['image']
            caption = form.cleaned_data['caption']

            # limit to one database record and image file.
            picture, created = Picture.objects.get_or_create(picture_id=1)
            if not created and picture.get_image_filename():
                try:
                    os.remove( picture.get_image_filename() )
                except OSError:
                    pass

            # save the image to the filesystem and set picture.image
            picture.save_image_file(
                uploadedImage.filename, 
                uploadedImage.content
            )

            # set the other fields and save it to the database
            picture.caption = caption
            picture.save()

            # finally, create a new, empty form so the 
            # user can upload another picture.
            form = PictureUploadForm()

    return render_to_response(
        'example/picture_upload.html',
        Context(dict( form=form, last_picture=picture) ) )

错误说:

global name 'Context' is not defined" at last line of my code. "views.py in picture_upload, line 111".

我该如何解决这个问题?

最佳答案

如果未定义,则未定义。您必须从 django.template 导入 Context。

在文件的顶部,输入

from django.template import Context

你不能神奇地希望所有变量都被定义...如果你没有导入它或以其他方式定义它,你认为你可以使用 PictureUploadForm 吗?

关于django - 在 django : getting "global name context is not defined" error 中上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12064540/

相关文章:

python - 将字符串转换为日期 django 模板

android - 在 Android 中从 OpenGL 捕获渲染图像并将其保存为 JPEG

PHP GD 库,将 2 张图像并排转换为 1 张

javascript - 使用带有变量的javascript更改图像src

python - 在 Django 中过滤超过 0000-00-00 日期的对象

django - 如何在 django 模型中创建一个值或外键的字段?

python - 如何使用 MariaDB 在 Django 中正确过滤 SIP 字符?

python - 如何查询名称包含python列表中任何单词的模型?

python - 在 Python 中将 base64 转换为图像

html - 屏幕分辨率和图像大小,如何正确