简体   繁体   English

如何在Django中上传图片

[英]How to upload an image in Django

I'm creating a new app in django environment. 我正在django环境中创建一个新的应用程序。 I want to upload image of user when he/she sign-up's in my app and i want to use the same image when he/she posts any comment in my app. 我要在用户注册我的应用程序时上传该用户的图像,并且当他/她在我的应用程序中发布任何评论时我要使用同一图像。 So how to integrate image-uploading code into my app code? 那么如何将图像上传代码集成到我的应用程序代码中呢?

You should be using UserProfile as has been answered here Django user profile and have the ImageField in there. 您应该使用UserProfile就像Django用户配置文件中回答的那样,并且其中包含ImageField。

Then create a ModelForm for the UserProfile and render it wherever you want the user to setup their profile and add a user image. 然后,为UserProfile创建一个ModelForm ,并在您希望用户设置其个人资料并添加用户图像的任何位置进行渲染。 The Model form will render the ImageField as a file upload field and have default handles for it. “模型”窗体会将ImageField呈现为文件上载字段,并具有其默认句柄。 Make sure you have MEDIA_URL configured in your settings.py . 确保您已MEDIA_URL在配置settings.py You would also need PIL installed in your path for the image upload to work. 您还需要在路径中安装PIL才能上载图像。

You can access the UserProfile instance for your user doing user.get_profile in your templates and as a callable elsewhere. 您可以在模板中以及其他位置将其作为user.get_profileuser访问UserProfile实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM