or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

admin.mdauth.mdcontrib.mddatabase-orm.mdforms.mdhttp.mdindex.mdmigrations.mdmypy-plugin.mdsignals.mdtemplates.mdtransactions.mdurls.mdviews.md

views.mddocs/

0

# View System

1

2

Django's view system provides flexible request handling through both function-based and class-based views, including generic views for common patterns like displaying lists, detail pages, and handling forms.

3

4

## Capabilities

5

6

### Base View Classes

7

8

Core view classes providing the foundation for all Django views.

9

10

```python { .api }

11

class View:

12

"""

13

Base class for all class-based views.

14

15

Provides dispatch method routing and basic HTTP method handling.

16

"""

17

http_method_names: list = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']

18

19

def __init__(self, **kwargs): ...

20

21

@classmethod

22

def as_view(cls, **initkwargs): ...

23

24

def setup(self, request: HttpRequest, *args, **kwargs) -> None: ...

25

def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

26

def http_method_not_allowed(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

27

def options(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

28

29

# HTTP method handlers

30

def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

31

def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

32

def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

33

def patch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

34

def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

35

def head(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

36

def trace(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

37

38

class TemplateView(TemplateResponseMixin, ContextMixin, View):

39

"""

40

Render a template with optional context data.

41

42

Generic view for displaying templates with context.

43

"""

44

def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

45

def get_context_data(self, **kwargs) -> dict: ...

46

47

class RedirectView(View):

48

"""

49

Redirect to a given URL or pattern name.

50

51

Supports permanent redirects and URL pattern resolution.

52

"""

53

permanent: bool = False

54

url: str = None

55

pattern_name: str = None

56

query_string: bool = False

57

58

def get_redirect_url(self, *args, **kwargs) -> str: ...

59

def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

60

def head(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

61

def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

62

def options(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

63

def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

64

def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

65

def patch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

66

```

67

68

### Display Views

69

70

Views for displaying model data and object listings.

71

72

```python { .api }

73

class ListView(MultipleObjectTemplateResponseMixin, BaseListView):

74

"""

75

Display a list of objects with pagination support.

76

77

Generic view for displaying querysets as paginated lists.

78

"""

79

model = None

80

queryset = None

81

paginate_by = None

82

paginate_orphans: int = 0

83

context_object_name: str = None

84

template_name: str = None

85

template_name_suffix: str = '_list'

86

allow_empty: bool = True

87

page_kwarg: str = 'page'

88

paginator_class = None

89

ordering = None

90

91

def get_queryset(self) -> QuerySet: ...

92

def get_context_data(self, **kwargs) -> dict: ...

93

def get_paginate_by(self, queryset: QuerySet) -> int: ...

94

def get_paginator(self, queryset: QuerySet, per_page: int, orphans: int = 0, allow_empty_first_page: bool = True): ...

95

def get_paginate_orphans(self) -> int: ...

96

def get_allow_empty(self) -> bool: ...

97

def get_context_object_name(self, object_list) -> str: ...

98

def paginate_queryset(self, queryset: QuerySet, page_size: int) -> tuple: ...

99

def get_ordering(self): ...

100

101

class DetailView(SingleObjectTemplateResponseMixin, BaseDetailView):

102

"""

103

Display a single object detail page.

104

105

Generic view for displaying individual model instances.

106

"""

107

model = None

108

queryset = None

109

slug_field: str = 'slug'

110

slug_url_kwarg: str = 'slug'

111

pk_url_kwarg: str = 'pk'

112

context_object_name: str = None

113

template_name: str = None

114

template_name_field: str = None

115

template_name_suffix: str = '_detail'

116

117

def get_object(self, queryset: QuerySet = None): ...

118

def get_queryset(self) -> QuerySet: ...

119

def get_slug_field(self) -> str: ...

120

def get_context_data(self, **kwargs) -> dict: ...

121

def get_context_object_name(self, obj) -> str: ...

122

```

123

124

### Form Views

125

126

Views for handling form display and processing.

127

128

```python { .api }

129

class FormView(TemplateResponseMixin, BaseFormView):

130

"""

131

Display a form and handle form processing.

132

133

Generic view for displaying and processing forms.

134

"""

135

form_class = None

136

success_url: str = None

137

template_name: str = None

138

139

def get_form_class(self): ...

140

def get_form(self, form_class=None): ...

141

def get_form_kwargs(self) -> dict: ...

142

def get_initial(self) -> dict: ...

143

def form_valid(self, form) -> HttpResponse: ...

144

def form_invalid(self, form) -> HttpResponse: ...

145

def get_context_data(self, **kwargs) -> dict: ...

146

def get_success_url(self) -> str: ...

147

148

class ProcessFormView(View):

149

"""

150

Base view for processing forms with GET and POST handling.

151

"""

152

def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

153

def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

154

def put(self, *args, **kwargs) -> HttpResponse: ...

155

```

156

157

### Model Edit Views

158

159

Views for creating, updating, and deleting model instances.

160

161

```python { .api }

162

class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):

163

"""

164

Create a new model instance with a form.

165

166

Generic view for creating new objects through forms.

167

"""

168

model = None

169

form_class = None

170

fields = None

171

success_url: str = None

172

template_name: str = None

173

template_name_suffix: str = '_form'

174

175

def get_object(self, queryset: QuerySet = None): ...

176

def form_valid(self, form) -> HttpResponse: ...

177

def get_form_class(self): ...

178

def get_form_kwargs(self) -> dict: ...

179

def get_success_url(self) -> str: ...

180

181

class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):

182

"""

183

Update an existing model instance with a form.

184

185

Generic view for editing existing objects through forms.

186

"""

187

model = None

188

form_class = None

189

fields = None

190

slug_field: str = 'slug'

191

slug_url_kwarg: str = 'slug'

192

pk_url_kwarg: str = 'pk'

193

success_url: str = None

194

template_name: str = None

195

template_name_suffix: str = '_form'

196

197

def get_object(self, queryset: QuerySet = None): ...

198

def form_valid(self, form) -> HttpResponse: ...

199

def get_form_class(self): ...

200

def get_form_kwargs(self) -> dict: ...

201

def get_success_url(self) -> str: ...

202

203

class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):

204

"""

205

Delete an existing model instance with confirmation.

206

207

Generic view for deleting objects with confirmation page.

208

"""

209

model = None

210

queryset = None

211

slug_field: str = 'slug'

212

slug_url_kwarg: str = 'slug'

213

pk_url_kwarg: str = 'pk'

214

success_url: str = None

215

template_name: str = None

216

template_name_suffix: str = '_confirm_delete'

217

218

def get_object(self, queryset: QuerySet = None): ...

219

def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

220

def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

221

def get_success_url(self) -> str: ...

222

```

223

224

### Date-Based Views

225

226

Views for displaying objects filtered by date and time.

227

228

```python { .api }

229

class ArchiveIndexView(MultipleObjectTemplateResponseMixin, BaseArchiveIndexView):

230

"""

231

Display archive index of date-based objects.

232

233

Top-level index page showing latest objects by date.

234

"""

235

date_field: str = None

236

allow_future: bool = False

237

allow_empty: bool = True

238

template_name_suffix: str = '_archive'

239

240

def get_dated_items(self) -> tuple: ...

241

def get_dated_queryset(self, **lookup) -> QuerySet: ...

242

def get_date_field(self) -> str: ...

243

def get_allow_future(self) -> bool: ...

244

245

class YearArchiveView(MultipleObjectTemplateResponseMixin, BaseYearArchiveView):

246

"""

247

Display objects for a specific year.

248

249

Archive view filtered by year with optional month navigation.

250

"""

251

year_format: str = '%Y'

252

date_field: str = None

253

make_object_list: bool = False

254

allow_future: bool = False

255

allow_empty: bool = False

256

template_name_suffix: str = '_archive_year'

257

258

def get_year_format(self) -> str: ...

259

def get_year(self) -> str: ...

260

def get_next_year(self, date): ...

261

def get_previous_year(self, date): ...

262

def get_dated_items(self) -> tuple: ...

263

def get_make_object_list(self) -> bool: ...

264

265

class MonthArchiveView(MultipleObjectTemplateResponseMixin, BaseMonthArchiveView):

266

"""

267

Display objects for a specific month.

268

269

Archive view filtered by year and month.

270

"""

271

year_format: str = '%Y'

272

month_format: str = '%b'

273

date_field: str = None

274

allow_future: bool = False

275

allow_empty: bool = False

276

template_name_suffix: str = '_archive_month'

277

278

def get_month_format(self) -> str: ...

279

def get_month(self) -> str: ...

280

def get_next_month(self, date): ...

281

def get_previous_month(self, date): ...

282

def get_dated_items(self) -> tuple: ...

283

284

class WeekArchiveView(MultipleObjectTemplateResponseMixin, BaseWeekArchiveView):

285

"""

286

Display objects for a specific week.

287

288

Archive view filtered by year and week.

289

"""

290

year_format: str = '%Y'

291

week_format: str = '%U'

292

date_field: str = None

293

allow_future: bool = False

294

allow_empty: bool = False

295

template_name_suffix: str = '_archive_week'

296

297

def get_week_format(self) -> str: ...

298

def get_week(self) -> str: ...

299

def get_next_week(self, date): ...

300

def get_previous_week(self, date): ...

301

def get_dated_items(self) -> tuple: ...

302

303

class DayArchiveView(MultipleObjectTemplateResponseMixin, BaseDayArchiveView):

304

"""

305

Display objects for a specific day.

306

307

Archive view filtered by year, month, and day.

308

"""

309

year_format: str = '%Y'

310

month_format: str = '%b'

311

day_format: str = '%d'

312

date_field: str = None

313

allow_future: bool = False

314

allow_empty: bool = False

315

template_name_suffix: str = '_archive_day'

316

317

def get_day_format(self) -> str: ...

318

def get_day(self) -> str: ...

319

def get_next_day(self, date): ...

320

def get_previous_day(self, date): ...

321

def get_dated_items(self) -> tuple: ...

322

323

class TodayArchiveView(MultipleObjectTemplateResponseMixin, BaseTodayArchiveView):

324

"""

325

Display objects for today's date.

326

327

Archive view filtered to current date.

328

"""

329

template_name_suffix: str = '_archive_today'

330

331

class DateDetailView(SingleObjectTemplateResponseMixin, BaseDateDetailView):

332

"""

333

Display a single object on a specific date.

334

335

Detail view that includes date-based filtering.

336

"""

337

year_format: str = '%Y'

338

month_format: str = '%b'

339

day_format: str = '%d'

340

date_field: str = None

341

allow_future: bool = False

342

template_name_suffix: str = '_detail'

343

344

def get_object(self, queryset: QuerySet = None): ...

345

```

346

347

### View Mixins

348

349

Mixins providing reusable functionality for views.

350

351

```python { .api }

352

class ContextMixin:

353

"""

354

Mixin for adding extra context to template views.

355

"""

356

extra_context: dict = None

357

358

def get_context_data(self, **kwargs) -> dict: ...

359

360

class TemplateResponseMixin:

361

"""

362

Mixin for views that render templates.

363

364

Provides template selection and response rendering.

365

"""

366

template_name: str = None

367

template_engine: str = None

368

response_class = None

369

content_type: str = None

370

371

def render_to_response(self, context: dict, **response_kwargs) -> HttpResponse: ...

372

def get_template_names(self) -> list: ...

373

374

class SingleObjectMixin(ContextMixin):

375

"""

376

Mixin for views that work with single model instances.

377

"""

378

model = None

379

queryset = None

380

slug_field: str = 'slug'

381

context_object_name: str = None

382

slug_url_kwarg: str = 'slug'

383

pk_url_kwarg: str = 'pk'

384

query_pk_and_slug: bool = False

385

386

def get_object(self, queryset: QuerySet = None): ...

387

def get_queryset(self) -> QuerySet: ...

388

def get_slug_field(self) -> str: ...

389

def get_context_object_name(self, obj) -> str: ...

390

def get_context_data(self, **kwargs) -> dict: ...

391

392

class MultipleObjectMixin(ContextMixin):

393

"""

394

Mixin for views that work with multiple objects.

395

"""

396

allow_empty: bool = True

397

queryset = None

398

model = None

399

paginate_by = None

400

paginate_orphans: int = 0

401

context_object_name: str = None

402

paginator_class = None

403

page_kwarg: str = 'page'

404

ordering = None

405

406

def get_queryset(self) -> QuerySet: ...

407

def get_ordering(self): ...

408

def paginate_queryset(self, queryset: QuerySet, page_size: int) -> tuple: ...

409

def get_paginate_by(self, queryset: QuerySet) -> int: ...

410

def get_paginator(self, queryset: QuerySet, per_page: int, orphans: int = 0, allow_empty_first_page: bool = True): ...

411

def get_paginate_orphans(self) -> int: ...

412

def get_allow_empty(self) -> bool: ...

413

def get_context_object_name(self, object_list) -> str: ...

414

def get_context_data(self, **kwargs) -> dict: ...

415

416

class FormMixin(ContextMixin):

417

"""

418

Mixin for views that handle forms.

419

"""

420

initial: dict = {}

421

form_class = None

422

success_url: str = None

423

prefix: str = None

424

425

def get_initial(self) -> dict: ...

426

def get_prefix(self) -> str: ...

427

def get_form_class(self): ...

428

def get_form(self, form_class=None): ...

429

def get_form_kwargs(self) -> dict: ...

430

def get_success_url(self) -> str: ...

431

def form_valid(self, form) -> HttpResponse: ...

432

def form_invalid(self, form) -> HttpResponse: ...

433

def get_context_data(self, **kwargs) -> dict: ...

434

435

class ModelFormMixin(FormMixin, SingleObjectMixin):

436

"""

437

Mixin for views that handle model forms.

438

"""

439

fields = None

440

441

def get_form_class(self): ...

442

def get_form_kwargs(self) -> dict: ...

443

def get_success_url(self) -> str: ...

444

def form_valid(self, form) -> HttpResponse: ...

445

446

class ProcessFormView(View):

447

"""

448

Base view for processing forms.

449

"""

450

def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

451

def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: ...

452

```

453

454

### Function-Based View Decorators

455

456

Decorators for enhancing function-based views.

457

458

```python { .api }

459

def require_http_methods(request_method_list: list):

460

"""

461

Decorator to require specific HTTP methods.

462

463

Args:

464

request_method_list: List of allowed HTTP methods

465

"""

466

467

def require_GET(view_func):

468

"""Decorator to require GET method."""

469

470

def require_POST(view_func):

471

"""Decorator to require POST method."""

472

473

def require_safe(view_func):

474

"""Decorator to require safe methods (GET, HEAD)."""

475

476

def condition(etag_func=None, last_modified_func=None):

477

"""

478

Decorator for conditional view processing.

479

480

Args:

481

etag_func: Function to calculate ETag

482

last_modified_func: Function to calculate last modified date

483

"""

484

485

def etag(etag_func):

486

"""

487

Decorator to add ETag headers.

488

489

Args:

490

etag_func: Function to calculate ETag value

491

"""

492

493

def last_modified(last_modified_func):

494

"""

495

Decorator to add Last-Modified headers.

496

497

Args:

498

last_modified_func: Function to calculate last modified date

499

"""

500

501

def vary_on_cookie(view_func):

502

"""Decorator to add Vary: Cookie header."""

503

504

def vary_on_headers(*headers):

505

"""

506

Decorator to add Vary headers.

507

508

Args:

509

headers: Header names to vary on

510

"""

511

512

def cache_control(**kwargs):

513

"""

514

Decorator to add Cache-Control headers.

515

516

Args:

517

**kwargs: Cache control directives

518

"""

519

520

def never_cache(view_func):

521

"""Decorator to prevent caching."""

522

523

def cache_page(timeout: int, cache: str = None, key_prefix: str = None):

524

"""

525

Decorator for page-level caching.

526

527

Args:

528

timeout: Cache timeout in seconds

529

cache: Cache backend name

530

key_prefix: Cache key prefix

531

"""

532

```

533

534

### Response Utilities

535

536

Utilities for creating specialized view responses.

537

538

```python { .api }

539

def render(request: HttpRequest, template_name: str, context: dict = None,

540

content_type: str = None, status: int = None, using: str = None) -> HttpResponse:

541

"""

542

Render template with context to HttpResponse.

543

544

Args:

545

request: HTTP request object

546

template_name: Template name to render

547

context: Template context dictionary

548

content_type: Response content type

549

status: HTTP status code

550

using: Template engine name

551

"""

552

553

def render_to_response(template_name: str, context: dict = None,

554

content_type: str = None, status: int = None, using: str = None) -> HttpResponse:

555

"""

556

Render template to HttpResponse without request context.

557

558

Args:

559

template_name: Template name to render

560

context: Template context dictionary

561

content_type: Response content type

562

status: HTTP status code

563

using: Template engine name

564

"""

565

566

def redirect(to, permanent: bool = False, *args, **kwargs) -> HttpResponse:

567

"""

568

Return redirect response to given URL or view.

569

570

Args:

571

to: URL, view name, or model instance to redirect to

572

permanent: Whether to use permanent redirect (301 vs 302)

573

*args: Arguments for URL resolution

574

**kwargs: Keyword arguments for URL resolution

575

"""

576

577

def get_object_or_404(klass, *args, **kwargs):

578

"""

579

Get object or raise Http404 if not found.

580

581

Args:

582

klass: Model class or Manager

583

*args: Lookup arguments

584

**kwargs: Lookup keyword arguments

585

"""

586

587

def get_list_or_404(klass, *args, **kwargs):

588

"""

589

Get object list or raise Http404 if empty.

590

591

Args:

592

klass: Model class or Manager

593

*args: Filter arguments

594

**kwargs: Filter keyword arguments

595

"""

596

```