0

I've gone through about 8 tutorials across YouTube and Online websites. I believe everything is configured correctly. However, when my objects are created it is not creating the media directory or storing the files. I have done the settings file, urls, views, models everything.

Here are relevant files:

// setting.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

// models.py
class Trip(models.Model):
    city = models.CharField(max_length= 255)
    country = models.CharField(max_length= 255)
    description = models.CharField(max_length= 255)
    creator = models.ForeignKey(User, related_name = 'trips_uploaded',on_delete= CASCADE, null=True)
    favoriter = models.ManyToManyField(User, related_name= 'fav_trips')
    photo = models.ImageField(null=True, blank =True, upload_to='trips')

// urls.py ( there were 2 ways to write media according to tutorial)
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('travelsiteapp.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

# urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

// views.py (lastly)
def tripAdd(request):
    form = TripForm()
    if request.method == 'POST':
        form = TripForm(request.POST, request.FILES)
        if form.is_valid():
            form.photo = form.cleaned_data["photo"]
            form.save()

    context = { 'form': form}
    return render(request, 'tripAdd.html', context)

// html/ form

    <form action="/createTrip"method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{form.as_p}}
        <input type="submit" value="submit">
    </form>

// forms.py 

from django import forms
from django.forms import ModelForm
from .models import Trip
from django import forms

class TripForm(ModelForm):
    city = forms.TextInput()
    country = forms.TimeField()
    description = forms.Textarea()
    photo = forms.ImageField()
    class Meta:
        model = Trip
        fields = ['city', 'country', 'description', 'photo']


// I have hit all the steps not sure whats wrong? & and installed pip pillow
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Keith Coye
  • 75
  • 1
  • 7

1 Answers1

0

Within your forms.py it's not necessary to recreate the existing fields from your model.

class TripForm(ModelForm):
     # city = forms.TextInput()
     # country = forms.TimeField()
     # description = forms.Textarea()
     # photo = forms.ImageField()
     # The above is not necessary here...

     class Meta:
          model = Trip
          # These it should be since the model does have these fields
          fields = ['city', 'country', 'description', 'photo'] 

However, within your views.py file in the tripAdd method, that line where I'm seeing where you have form.photo = form.cleaned_data['photo'] that's not necessary also. Just comment/remove that line and just call the save method on the form.

form = TripForm(request.POST, request.FILES)
if form.is_valid():
     # form.photo = form.cleaned_data["photo"]
     form.save()

Additionally, I'd suggest using a / when dealing with folders as best practice.

On the Trip model:

photo = models.ImageField(upload_to='trips/', null=True, blank =True)

In the project's urls.py add the following:

urlpatterns = [
     path('admin/', admin.site.urls),
     path('', include('travelsiteapp.urls'))
] # + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# Adding the media url/path if Debug is true
if settings.DEBUG:
     urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 
Damoiskii
  • 1,328
  • 1
  • 5
  • 20