| | 287 | |
|---|
| | 288 | Template style |
|---|
| | 289 | -------------- |
|---|
| | 290 | |
|---|
| | 291 | * In Django template code, put one (and only one) space between the curly |
|---|
| | 292 | brackets and the tag contents. |
|---|
| | 293 | |
|---|
| | 294 | Do this:: |
|---|
| | 295 | |
|---|
| | 296 | {{ foo }} |
|---|
| | 297 | |
|---|
| | 298 | Don't do this:: |
|---|
| | 299 | |
|---|
| | 300 | {{foo}} |
|---|
| | 301 | |
|---|
| | 302 | View style |
|---|
| | 303 | ---------- |
|---|
| | 304 | |
|---|
| | 305 | * In Django views, the first parameter in a view function should be called |
|---|
| | 306 | ``request``. |
|---|
| | 307 | |
|---|
| | 308 | Do this:: |
|---|
| | 309 | |
|---|
| | 310 | def my_view(request, foo): |
|---|
| | 311 | # ... |
|---|
| | 312 | |
|---|
| | 313 | Don't do this:: |
|---|
| | 314 | |
|---|
| | 315 | def my_view(req, foo): |
|---|
| | 316 | # ... |
|---|
| | 317 | |
|---|
| | 318 | Model style |
|---|
| | 319 | ----------- |
|---|
| | 320 | |
|---|
| | 321 | * Field names should be all lowercase, using underscores instead of |
|---|
| | 322 | camelCase. |
|---|
| | 323 | |
|---|
| | 324 | Do this:: |
|---|
| | 325 | |
|---|
| | 326 | class Person(models.Model): |
|---|
| | 327 | first_name = models.CharField(maxlength=20) |
|---|
| | 328 | last_name = models.CharField(maxlength=40) |
|---|
| | 329 | |
|---|
| | 330 | Don't do this:: |
|---|
| | 331 | |
|---|
| | 332 | class Person(models.Model): |
|---|
| | 333 | FirstName = models.CharField(maxlength=20) |
|---|
| | 334 | Last_Name = models.CharField(maxlength=40) |
|---|
| | 335 | |
|---|
| | 336 | * The ``class Meta`` should appear *after* the fields are defined, with |
|---|
| | 337 | a single blank line separating the fields and the class definition. |
|---|
| | 338 | |
|---|
| | 339 | Do this:: |
|---|
| | 340 | |
|---|
| | 341 | class Person(models.Model): |
|---|
| | 342 | first_name = models.CharField(maxlength=20) |
|---|
| | 343 | last_name = models.CharField(maxlength=40) |
|---|
| | 344 | |
|---|
| | 345 | class Meta: |
|---|
| | 346 | verbose_name_plural = 'people' |
|---|
| | 347 | |
|---|
| | 348 | Don't do this:: |
|---|
| | 349 | |
|---|
| | 350 | class Person(models.Model): |
|---|
| | 351 | FirstName = models.CharField(maxlength=20) |
|---|
| | 352 | Last_Name = models.CharField(maxlength=40) |
|---|
| | 353 | class Meta: |
|---|
| | 354 | verbose_name_plural = 'people' |
|---|
| | 355 | |
|---|
| | 356 | Don't do this, either:: |
|---|
| | 357 | |
|---|
| | 358 | class Person(models.Model): |
|---|
| | 359 | class Meta: |
|---|
| | 360 | verbose_name_plural = 'people' |
|---|
| | 361 | |
|---|
| | 362 | FirstName = models.CharField(maxlength=20) |
|---|
| | 363 | Last_Name = models.CharField(maxlength=40) |
|---|
| | 364 | |
|---|
| | 365 | * The order of model inner classes and standard methods should be as |
|---|
| | 366 | follows (noting that these are not all required): |
|---|
| | 367 | |
|---|
| | 368 | * All database fields |
|---|
| | 369 | * ``class Meta`` |
|---|
| | 370 | * ``class Admin`` |
|---|
| | 371 | * ``def __str__()`` |
|---|
| | 372 | * ``def save()`` |
|---|
| | 373 | * ``def get_absolute_url()`` |
|---|
| | 374 | * Any custom methods |
|---|
| | 375 | |
|---|
| | 376 | * If ``choices`` is defined for a given model field, define the choices as |
|---|
| | 377 | a tuple of tuples, with an all-uppercase name, either near the top of the |
|---|
| | 378 | model module or just above the model class. Example:: |
|---|
| | 379 | |
|---|
| | 380 | GENDER_CHOICES = ( |
|---|
| | 381 | ('M', 'Male'), |
|---|
| | 382 | ('F', 'Female'), |
|---|
| | 383 | ) |
|---|