0

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>

enter image description here

azhar
  • 351
  • 3
  • 13
  • can you please show the code for your urls.py ? – Tms91 Jun 11 '20 at 12:41
  • 1
    this is detail of my code https://stackoverflow.com/q/62323763/5945175 – azhar Jun 12 '20 at 06:14
  • Please also show the code for your form. Could it be that in
    tag, the attribute enctype="multipart/form-data" is missing? Take a look here https://simpleisbetterthancomplex.com/tutorial/2016/08/01/how-to-upload-files-with-django.html
    – Tms91 Jun 12 '20 at 07:18
  • I am getting same issue. Did you able solve this? @azhar – Satyendra Chauhan Aug 01 '20 at 08:57
  • https://stackoverflow.com/questions/63328969/cannot-upload-media-files-on-cpanel-using-django This stackoverflow has a solution that works. – Umer Sep 24 '20 at 10:48

3 Answers3

0

The name you gave to your url page for event is eventz, but in your url bar there is pattern event.

In your urls.py you should have something like:

path('event', views.event, name="eventz"),

change it to:

path('event', views.event, name="event"),

and it should work.

Tms91
  • 3,456
  • 6
  • 40
  • 74
  • thanks for help. still not working. i have detailed my question. kindly recheck with detail. will be thank full to you – azhar Jun 12 '20 at 04:23
0

update your urls list with media url path something like this :-

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

update your setting.py like this

MEDIA_ROOT = '/home/dan/mysite/media/'

MEDIA_URL = '/media/'

try to execute.

python manage.py collectstatic
  • thanks for help. still not working. i have detailed my question. kindly recheck with detail. will be thank full to you – azhar Jun 12 '20 at 04:23
0

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

<form action="/eventz/eventz/" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{form.name}}
    {{form.image}}
    <input type="submit" value="Save">
</form>

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)
azhar
  • 351
  • 3
  • 13