locally its working fine there is no error
when i deploy django on cpanel(shared hosting) all things are working fine
but when i submit the form without image it submitted successfully.
if i choose an image and submit the form. will get page not found (404) error
i tried to disable imageField from models.py and from every place where it has used. then i checked its working fine with imageField its not working
i have changed the media_root and media_url with static_root and url. but still its not working.
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'
MEDIA_URL = '/media/'
MEDIA_ROOT = '/home/intelexcel/public_html/media'
# LOGOUT_REDIRECT_URL = 'login'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
events.html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.name}}
{{form.image}}
<input type="submit" value="Save">
</form>
3
Answers
there was an error with path.
i have changed the action url of my form if the form will be submit on sec.intelexcel.com/event i changed the action when form is submitted sec.intelexcel.com/eventz/eventz/
it works for me
The name you gave to your url page for event is
eventz
, but in your url bar there is patternevent
.In your urls.py you should have something like:
change it to:
and it should work.
update your urls list with media url path something like this :-
update your setting.py like this
try to execute.