简体   繁体   中英

Django: Page not found , Raised by:django.views.static.serve

I am trying to display a static image in django. When I select image using django admin portal then it's works fine. it is displaying image. But when I select image from my front-end page, I get: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/media/destination-2.jpg Raised by: django.views.static.server

Here are my codes: urls.py

from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from mainapp import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('mainapp.urls')),
]

admin.site.site_header = "Login to our web Portal"
admin.site.site_title = "This is Admin Portal"
admin.site.index_title = "Welcome to Amnet"

#urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

settings.py

STATICFILES_DIRS=[os.path.join(BASE_DIR,'static')
]

MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')

models.py

from django.db import models

# Create your models here.
class Amnet(models.Model):
    imagee = models.FileField(default='bg_2.jpg')

views.py

from django.shortcuts import render
from mainapp.models import Amnet
# Create your views here.
def index(request):
    if(request.method == "POST"):
        imagee= request.POST.get('imagee')
        am = Amnet(imagee=imagee)
        am.save()
        return render(request,'image-main.html')
    else:
        return render(request,'image-main.html')

html page

<form action="" method="POST">
    {% csrf_token %} 
    <td><input type="file" name="imagee" id="file"></td>
    <td><img src="" id="profile-img-tag" width="200px" /></td>
</tr>

</table>
<input type="Submit" value="Submit" id="btn"><br>  
</form>

Use enctype="multipart/form-data" to handle file uploads. Add this attribute in the form tag in HTML

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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