但是当我提交没有图像的表单时,它成功提交了。如果我选择一张图片并提交表格。将得到找不到页面(404)错误
我试图从models.py及其使用过的每个位置禁用imageField。然后我检查了它与imageField的工作正常,不工作
我已使用static_root和url更改了media_root和media_url。但仍然无法正常工作。
models.py
from django.db import models from django.urls import reverse_lazy # Create your models here. class Eventz(models.Model): name = models.CharField(max_length=50) image = models.ImageField(upload_to='events') class Meta: ordering = ['-id'] db_table = 'eventz' def get_absolute_url(self): return reverse_lazy('eventz') def __str__(self): return self.name
views.py
from django.shortcuts import render from django.views.generic import CreateView, UpdateView, DeleteView, ListView, TemplateView, RedirectView from .models import Eventz class EventzCreate(CreateView): model = Eventz fields = ['name', 'image'] template_name = 'events.html' def get_context_data(self, **kwargs): context = super(EventzCreate, self).get_context_data(**kwargs) context['events'] = Eventz.objects.all() return context
urls.py
from django.contrib import admin from django.urls import path, include from . import settings from django.contrib.staticfiles.urls import static from upload import views from dashboard1.views import EventzCreate urlpatterns = [ path('admin/', admin.site.urls), path('', views.UploadCreate.as_view(), name="upload" ), path('eventz/', EventzCreate.as_view(), name='eventz'), ] if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/' STATIC_ROOT = '/home/intelexcel/public_html/static' STATIC_URL = '/media/' STATIC_ROOT = '/home/intelexcel/public_html/media' # LOGOUT_REDIRECT_URL = 'login' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
本地工作正常,当我在cpanel(共享主机)上部署django时,一切正常,但是当我提交没有图像的表单时,它成功提交了。如果我选择...
eventz
,但在网址栏中显示的模式是event
。在您的urls.py中,您应该有类似的内容: