How To Add Image Url Field To An Html Form In Django
I am creating a donation web application. Users are able to fill out a form and there donation is submitted into the database, I was wondering how I can have the user submit a url
Solution 1:
def saveImage(request):
if request.method == "POST":
image = request.FILES.get('image')
new_image = <Image_Model>.objects.create(
image = image
)
return redirect('/somewhere/')
return render(request, 'new_image.html',)
Solution 2:
- Create a folder in the app level of your project...name it "static"
- Inside the "static" folder, create another folder, name it "images"
- Open the SETTINGS.PY file and paste the code below
SETTINGS.PY
MEDIA_URL = '/images/'MEDIA_ROOT = BASE_DIR / 'static/images'
Open the URLS.PY file in the same folder as the SETTINGS.PY and paste the code below
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
HTML FORM
<form method="POST" enctype="multipart/form-data" action="{%destination url%}">
<input name="image" type="file">
</form>
Post a Comment for "How To Add Image Url Field To An Html Form In Django"