Home > Mobile >  'QueryDict' object has no attribute 'method'
'QueryDict' object has no attribute 'method'

Time:12-29

So Im building a real estate website fro school. Im working on my CRUD functionality on the front end. When i try to post a new listing i get the error.

'QueryDict' object has no attribute 'method'

I don't really know what the problem is. Any tips and i will be forever grateful. Also im new to this whole stack overflow thing, so please let me know if you need any more info from me.

models.py

from django.db import models
from datetime import datetime
from realtors.models import Realtor


class Listing(models.Model):
    realtor = models.ForeignKey(Realtor, on_delete=models.DO_NOTHING)
    title = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    zipcode = models.CharField(max_length=20)
    description = models.TextField(blank=True)
    price = models.IntegerField()
    bedrooms = models.IntegerField()
    bathrooms = models.DecimalField(max_digits=2, decimal_places=1)
    garage = models.IntegerField(default=0)
    sqft = models.IntegerField()
    
    photo_main = models.ImageField(upload_to='photos/%Y/%m/%d/')
    photo_1 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_2 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_3 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_4 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_5 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    photo_6 = models.ImageField(upload_to='photos/%Y/%m/%d/', blank=True)
    is_published = models.BooleanField(default=True)
    list_date = models.DateTimeField(default=datetime.now, blank=True)

    def __str__(self):
        return self.title

forms.py

from django.forms import ModelForm
from listings.models import Listing 


class listingForm(ModelForm):
    class Meta:
        model = Listing
        fields = '__all__'

views.py

def createListing(request):

    form = listingForm()
    if request.method == 'POST':
        form = createListing(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')

    context = {'form': form}

    return render(request, 'accounts/create_listing.html', {'form': form})

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('login', views.login, name='login'),
    path('register', views.register, name='register'),
    path('logout', views.logout, name='logout'),
    path('dashboard', views.dashboard, name='dashboard'),
    path('admin', views.createListing, name='admin'),
]

CodePudding user response:

The name of the form is listingForm, not createListing, you thus call this with:

def createListing(request):
    form = listingForm()
    if request.method == 'POST':
        form = listingForm(request.POST, request.FILES)  # listingForm
        if form.is_valid():
            form.save()
            return redirect('/')

    context = {'form': form}

    return render(request, 'accounts/create_listing.html', {'form': form})
  • Related