Currently I have a script that allows a user to add new Hotel using a Form Here is the script.
I get the following Error Exception AddHotelForm.Meta.fields 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 Value:
AddHotelForm.Meta.fields cannot be a string. Did you mean to type: ('name',)?
Exception Location: /usr/local/lib/python2.7/dist-packages/django/forms/models.py in new, line 261
I Seem not to get it I'have checked it several times and don't seem to find a solution to 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%}
So the problem seems to be exactly what is stated in the error message.
Your problem is that this:
("name")
is not the same as this:
("name",)
Try it:
>>> type(("name"))
<type 'str'>
>>> type(("name",))
<type 'tuple'>
So to fix it change your form definition:
class AddHotelForm(forms.ModelForm):
class Meta:
models = hotel # This should be "model" not "models".
fields = ('name',)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With