无法使用表单将图像上传到Django:404 POST /未找到。

huangapple go评论61阅读模式
英文:

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.

无法使用表单将图像上传到Django:404 POST /未找到。

I followed these tutorials.

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.

无法使用表单将图像上传到Django:404 POST /未找到。

I followed these tutorials.

This is the html code

 &lt;form action=&quot;.&quot; method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
            {% csrf_token %}
            {{form.as_p}}
            &lt;button type=&quot;submit&quot; class=&quot;btn btn-lg btn-success&quot;&gt;Upload&lt;/button&gt;
            &lt;/form&gt;

settings.py

STATIC_URL = &#39;static/&#39;
MEDIA_URL = &#39;/media/&#39;
MEDIA_ROOT = os.path.join(BASE_DIR,&#39;media&#39;)
STATICFILES_DIRS = (os.path.join(BASE_DIR,&quot;static&quot;), )

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=&quot;userimages/&quot;)
    

forms.py

from django import forms
from .models import *

class MovieImageForm(forms.ModelForm):
    class Meta:
        model=MovieImage
        fields=(&#39;image_upload&#39;,)
        labels={
            &#39;image_upload&#39;:&#39;&#39;,
        }

views.py

from django.shortcuts import render
from .forms import MovieImageForm
from .models import *
# Create your views here.
def startlighting(request):
    return render(request,&#39;Lightings.html&#39;)

def index(request):
    if request.method == &quot;POST&quot;:
        form=MovieImageForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()
            return render(request,&quot;Lightings.html&quot;)
    else: 
        form=MovieImageForm()
    return render(request,&#39;Lightings.html&#39;,{&#39;form&#39;: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(&#39;admin/&#39;, admin.site.urls),
    path(&quot;home&quot;,start),
    path(&quot;About&quot;,aboutstart),
    path(&quot;Lightings&quot;,startlighting),
    path(&quot;Lightings&quot;,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(&quot;Lightings&quot;,index)
]

change it to

urlpatterns = [
    path(&quot;&quot;,index)
]

huangapple
  • 本文由 发表于 2023年5月7日 04:49:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定