How to fix Meta.fields cannot be a string. Did you mean to type: ('name')

I currently have a script that allows the user to add a new hotel using the form. Here is the script.

I get the following AddHotelForm.Meta.fields error exception cannot be a string. Did you mean to type: ('name',)?

Request Method: GET Request URL: 8000 / Hotel / New Django Version: 1.6.2 Exception Type: TypeError Exception:

AddHotelForm.Meta.fields cannot be a string. Did you mean to type: ('name',)?

Exception Place: /usr/local/lib/python2.7/dist-packages/django/forms/models.py in the new , line 261

It seems that this was not the case, I checked it several times and did not seem to find a solution for it.

views.py

from django.shortcuts import render_to_response
from forms import AddHotelForm
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from hotel.models import hotel

def create(request):

    if request.POST:

         form = AddHotelForm(request.POST)

         if form.is_valid():

            form.save()

            return HttpResponseRedirect('/articles/all/')

    else:

         form = AddHotelForm()

    args = {}
    args.update(csrf(request))

    args['form'] = form

    return render_to_response('add_hotel.html', args)

models.py

from django.db import models
from user.models import user


class hotel(models.Model):

    publisher = models.ForeignKey(user)
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)
    creation_date = models.DateField(auto_now_add=True)

    def __unicode__(self):
        return self.name

forms.py

from django import forms
from models import hotel


class AddHotelForm(forms.ModelForm):

    class Meta:
        models = hotel
        fields = ('name')

add_hotel.html

{% block content%}
<html>
<head>
    <title></title>
    <style type="text/css">
        li {
            padding:5px;
            margin-bottom: 5px;
            list-style: none;

        }

        .errorlist {
            background-color: red;
            color:white;
            padding: 1px;
            width:150px;
            margin-bottom: 5px;
            font-size: 12px;
            font-family: arial, serif;

        }

        label {
            display: block;
            font-size: 12px;
            font-family: arial, serif;
        }
    </style>
</head>
<body>
    <form action="/hotel/new/" method="post">
        {% csrf_token %}
        <uL style="display:block;">
            {{form.as_ul}}
        </ul>
        <input type="submit" name="submit" value="Create Hotel">
    </form>
</body>
</html>
 {% endblock%}
+4
1

, , , , .

, :

("name")

:

("name",)

:

>>> type(("name"))
<type 'str'>
>>> type(("name",))
<type 'tuple'>

, , :

class AddHotelForm(forms.ModelForm):

    class Meta:
        models = hotel # This should be "model" not "models".
        fields = ('name',)
+16

Source: https://habr.com/ru/post/1529792/


All Articles