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

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

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

  1. <form action="." method="POST" enctype="multipart/form-data">
  2. {% csrf_token %}
  3. {{form.as_p}}
  4. <button type="submit" class="btn btn-lg btn-success">Upload</button>
  5. </form>

settings.py

  1. STATIC_URL = 'static/'
  2. MEDIA_URL = '/media/'
  3. MEDIA_ROOT = os.path.join(BASE_DIR,'media')
  4. STATICFILES_DIRS = (os.path.join(BASE_DIR,"static"), )

models.py under the app Lightings

  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. class MovieImage(models.Model):
  4. image_upload=models.ImageField(null=True,blank=True,upload_to="userimages/")

forms.py

  1. from django import forms
  2. from .models import *
  3. class MovieImageForm(forms.ModelForm):
  4. class Meta:
  5. model=MovieImage
  6. fields=('image_upload',)
  7. labels={
  8. 'image_upload':'',
  9. }

views.py

  1. from django.shortcuts import render
  2. from .forms import MovieImageForm
  3. from .models import *
  4. # Create your views here.
  5. def startlighting(request):
  6. return render(request,'Lightings.html')
  7. def index(request):
  8. if request.method == "POST":
  9. form=MovieImageForm(request.POST,request.FILES)
  10. if form.is valid():
  11. form.save()
  12. return render(request,"Lightings.html")
  13. else:
  14. form=MovieImageForm()
  15. return render(request,'Lightings.html',{'form':form})

urls.py at project level

  1. from django.contrib import admin
  2. from django.urls import path,include
  3. from homepage.views import start
  4. from About.views import aboutstart
  5. from Lightings.views import startlighting
  6. from django.conf import settings
  7. from django.conf.urls.static import static
  8. from Lightings.views import *
  9. urlpatterns = [
  10. path('admin/', admin.site.urls),
  11. path("home",start),
  12. path("About",aboutstart),
  13. path("Lightings",startlighting),
  14. path("Lightings",index)
  15. ]
  16. if settings.DEBUG:
  17. 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

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

settings.py

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

models.py under the app Lightings

  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. class MovieImage(models.Model):
  4. image_upload=models.ImageField(null=True,blank=True,upload_to=&quot;userimages/&quot;)

forms.py

  1. from django import forms
  2. from .models import *
  3. class MovieImageForm(forms.ModelForm):
  4. class Meta:
  5. model=MovieImage
  6. fields=(&#39;image_upload&#39;,)
  7. labels={
  8. &#39;image_upload&#39;:&#39;&#39;,
  9. }

views.py

  1. from django.shortcuts import render
  2. from .forms import MovieImageForm
  3. from .models import *
  4. # Create your views here.
  5. def startlighting(request):
  6. return render(request,&#39;Lightings.html&#39;)
  7. def index(request):
  8. if request.method == &quot;POST&quot;:
  9. form=MovieImageForm(request.POST,request.FILES)
  10. if form.is_valid():
  11. form.save()
  12. return render(request,&quot;Lightings.html&quot;)
  13. else:
  14. form=MovieImageForm()
  15. return render(request,&#39;Lightings.html&#39;,{&#39;form&#39;:form})

urls.py at project level

  1. from django.contrib import admin
  2. from django.urls import path,include
  3. from homepage.views import start
  4. from About.views import aboutstart
  5. from Lightings.views import startlighting
  6. from django.conf import settings
  7. from django.conf.urls.static import static
  8. from Lightings.views import *
  9. urlpatterns = [
  10. path(&#39;admin/&#39;, admin.site.urls),
  11. path(&quot;home&quot;,start),
  12. path(&quot;About&quot;,aboutstart),
  13. path(&quot;Lightings&quot;,startlighting),
  14. path(&quot;Lightings&quot;,index)
  15. ]
  16. if settings.DEBUG:
  17. 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中,将其更改为:

  1. urlpatterns = [
  2. path("", index)
  3. ]
英文:

You are trying to hit the wrong url.
In urls.py, instead of

  1. urlpatterns = [
  2. path(&quot;Lightings&quot;,index)
  3. ]

change it to

  1. urlpatterns = [
  2. path(&quot;&quot;,index)
  3. ]

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:

确定