英文:
Not able to upload images to Django with form: 404 POST / not found
问题
以下是您要翻译的内容:
"I was trying to create a webpage where users can upload their images and store them in Django but for some reason am not able to upload it.
This is the html code
<form action="." method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-lg btn-success">Upload</button>
</form>
settings.py
STATIC_URL = 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR,"static"), )
models.py under the app Lightings
from django.db import models
from django.contrib.auth.models import User
class MovieImage(models.Model):
image_upload=models.ImageField(null=True,blank=True,upload_to="userimages/")
forms.py
from django import forms
from .models import *
class MovieImageForm(forms.ModelForm):
class Meta:
model=MovieImage
fields=('image_upload',)
labels={
'image_upload':'',
}
views.py
from django.shortcuts import render
from .forms import MovieImageForm
from .models import *
# Create your views here.
def startlighting(request):
return render(request,'Lightings.html')
def index(request):
if request.method == "POST":
form=MovieImageForm(request.POST,request.FILES)
if form.is valid():
form.save()
return render(request,"Lightings.html")
else:
form=MovieImageForm()
return render(request,'Lightings.html',{'form':form})
urls.py at project level
from django.contrib import admin
from django.urls import path,include
from homepage.views import start
from About.views import aboutstart
from Lightings.views import startlighting
from django.conf import settings
from django.conf.urls.static import static
from Lightings.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path("home",start),
path("About",aboutstart),
path("Lightings",startlighting),
path("Lightings",index)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
希望这些对您有所帮助。如果您需要进一步的信息或翻译,请随时告诉我。
英文:
I was trying to create a webpage where users can upload their images and store them in Django but for some reason am not able to upload it.
This is the html code
<form action="." method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-lg btn-success">Upload</button>
</form>
settings.py
STATIC_URL = 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
STATICFILES_DIRS = (os.path.join(BASE_DIR,"static"), )
models.py under the app Lightings
from django.db import models
from django.contrib.auth.models import User
class MovieImage(models.Model):
image_upload=models.ImageField(null=True,blank=True,upload_to="userimages/")
forms.py
from django import forms
from .models import *
class MovieImageForm(forms.ModelForm):
class Meta:
model=MovieImage
fields=('image_upload',)
labels={
'image_upload':'',
}
views.py
from django.shortcuts import render
from .forms import MovieImageForm
from .models import *
# Create your views here.
def startlighting(request):
return render(request,'Lightings.html')
def index(request):
if request.method == "POST":
form=MovieImageForm(request.POST,request.FILES)
if form.is_valid():
form.save()
return render(request,"Lightings.html")
else:
form=MovieImageForm()
return render(request,'Lightings.html',{'form':form})
urls.py at project level
from django.contrib import admin
from django.urls import path,include
from homepage.views import start
from About.views import aboutstart
from Lightings.views import startlighting
from django.conf import settings
from django.conf.urls.static import static
from Lightings.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path("home",start),
path("About",aboutstart),
path("Lightings",startlighting),
path("Lightings",index)
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案1
得分: 0
你正在将请求发送到错误的网址。你应该访问http://127.0.0.1/8000/Lightings网址。在这个网址下,你已经注册了两个基于函数的视图。
英文:
You're sending your request to the wrong url. You should go to http://127.0.0.1/8000/Lightings url. You have registered two function based views under this url.
答案2
得分: 0
你尝试点击错误的网址。
在urls.py中,将其更改为:
urlpatterns = [
path("", index)
]
英文:
You are trying to hit the wrong url.
In urls.py, instead of
urlpatterns = [
path("Lightings",index)
]
change it to
urlpatterns = [
path("",index)
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论