skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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)
    

  2. 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.

    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search