Python Class method definition : unexpected indent [duplicate]

2024/11/15 11:32:18

I am getting started with Django and Python so naturally I'm doing the polls project tutorial. I am working under Windows 7 with Python 2.7.9 and Django 1.3.7

I have this piece of code (with line numbers under brackets):

(3) class Poll(models.Model):
(4)     question = models.CharField(max_length=200)
(5)     pub_date = models.DateTimeField('date published')
(6)     def __unicode__(self):
(7)         return self.question

and when trying to run "manage.py shell" I get the following error :

File "C:\Users...\mysite\polls\models.py", line 6

def unicode(self)

^

IndentationError: unexpected indent

What am I doing wrong ?

Answer

Your class header is in same indentation with its content , you need to refine the indentation (as a pythonic way use 4 space for indentation ):

class Poll(models.Model):question = models.CharField(max_length=200)pub_date = models.DateTimeField('date published')def __unicode__(self):return self.question
https://en.xdnf.cn/q/119662.html

Related Q&A

pandas python - round() not behaving correctly

Im rounding values in a dataframe to 1 decimal place. Here is the dfVren 2015 Hsten 2014 Vren 2014 Question 1) Maten r vllagad oc…

Converting dictionary into string

d={a:Apple,b:ball,c:cat}The above dictionary I have and I want my Output like the below-mentioned resultres="a=Apple,b=ball,c=cat"Is it possible in a pythonic way then please answer it I have…

Django Getting QuerySet.values() based on condition

Lets say I have a model Class Parent and a Class Child. And child has a field called status and a ForeignKey relationship to Parent.Lets say I retrieve one parent by calling filter (so as to have a Que…

Condition checking in python with user input [duplicate]

This question already has answers here:If...else statement issue with raw_input on Python(2 answers)Closed 8 years ago.I tried taking an input from keyboard. the checking that input with an if else sta…

Triangle of T in Python

EDIT ** I cant multiply strings by an integer. Its for a homework and those were the instructions **I need to do a triangle in python using for loops or while loops(mandatory). The final output should …

Finding prime project euler

Find the 10,001st prime number.I am trying to do Project Euler problems without using copying and pasting code I dont understand. I wrote code that finds whether a number is prime or not and am trying …

Splitting a python string

I have a string in python that I want to split in a very particular manner. I want to split it into a list containing each separate word, except for the case when a group of words are bordered by a par…

file modifiaction and manipulation

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it …

Get quantitative value for color on two-color scale

I have run a chemical test that produces a color depending on how much of a given chemical is in the sample. It is green if there is no chemical, and yellow if there is a saturating amount of chemical.…

how to save python session input and output [duplicate]

This question already has answers here:How to save a Python session, including input and output, as a text?(4 answers)Closed 2 years ago.All of the ways which discussed this question save the history …