Uncategorized

Python – Django Beginner – Form to submit data to my database – Cannot find answer


I am quite new to Python – Django and I have only spent my spare time learning.

I have successfully created a user registration form that posts data to my postgresql database:

My code looks like this:

Views:

        def signupr(request):
            if request.method == 'POST':
                username = request.POST\['username'\]
                email = request.POST\['email'\]
                password = request.POST\['password'\]
                password2 = request.POST\['password2'\]
        
                if password == password2:
                    if User.objects.filter(email=email).exists():
                        messages.info(request, 'Email Already Exists')
                        return redirect('registration')
                    elif User.objects.filter(username=username).exists():
                        messages.info(request, 'Username Already Exists')
                        return redirect('registration')
                    else:
                        user = User.objects.create_user(username=username, email=email,    password=password)
                        user.save();
                        return redirect('dashboard')
                else:
                    messages.info(request, 'Passwords Do Not Match')
                    return redirect('registration')
            else:
                return render(request, 'registration_form.html')

(very basic but it works)

I have a form that successfully creates users to a database.
Note that Django understands “User.objects.create_user” and creates a new user row for me.

The problem comes in when I try to use the same logic to create a property listing for example

I have a basic version of my model called “Properties” :

        `
        class Properties(models.Model):
            title = models.CharField(max_length=100, default="Not Set")
            feat_image = models.ImageField(upload_to='images', default="/images/scrolltop.png")
            gallery_images = models.ImageField(upload_to='images', default=False)
            external_property_listing_url = models.CharField(max_length=255)
            property_rules = models.CharField(max_length=500)
        `

Now when I try to create this in my views:

                def addpropertylisting(request):
                    if request.method == 'POST':
                        title = request.POST['title']
                
                        property = Properties.object.create(title=title)
                        property.save();
                        return redirect('dashboard')
                    else:
                        return redirect('list_your_property')
  • Django does not understand what I am trying to do and spits out the error: “type object ‘Properties’ has no attribute ‘object'”

Is there a way (Without using the django forms.py)
For me to use an HTML form to submit the property data to my database?

I would greatly appreciate any assistance in this regard.

Thank you in advance
Alexiss98

I have searched around on the internet and the only suggestions I can find is to use the built in Django ModelForms – I want to use my own HTML form however to accomplish this goal



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *