id
stringlengths
16
145
text
stringlengths
1
179k
title
stringclasses
1 value
Django_meta/classbasedviews_54_0.txt
# Additional Information
Django_meta/tutorialdjango_158_0.txt
Static files are pieces of content that your web app returns as-is for certain requests, such as CSS files. Serving static files requires that the ` INSTALLED_APPS ` list in ` settings.py ` contains ` django.contrib.staticfiles ` , which is included by default.
Django_meta/tutorialdjango_207_0.txt
"description": "Boilerplate template that extends layout.html" },
Django_meta/tutorialdjango_311_0.txt
2. In the terminal, run ` pip freeze > requirements.txt ` to create the ` requirements.txt ` file in your project folder.
Django_meta/tutorialdjango_136_0.txt
During your work with Django or any other library, you may want to examine the code in those libraries themselves. VS Code provides two convenient commands that navigate directly to the definitions of classes and other objects in any code:
Django_meta/classbasedviews_28_0.txt
The second, more powerful way to use generic views is to inherit from an existing view and override attributes (such as the ` template_name ` ) or methods (such as ` get_context_data ` ) in your subclass to provide new values or methods. Consider, for example, a view that just displays one template, ` about.html ` ....
Django_meta/tutorialdjango_209_0.txt
5. Now, whenever you start typing the snippet's prefix, such as ` djext ` , VS Code provides the snippet as an autocomplete option, as shown in the next section. You can also use the **Insert Snippet** command to choose a snippet from a menu.
Django_meta/classbasedviews_63_0.txt
* [ Django 5.0 documentation ](https://docs.djangoproject.com/en/5.0/) * [ Using Django ](../) * Class-based views
Django_meta/classbasedviews_50_0.txt
Django will automatically detect asynchronous views and run them in an asynchronous context. You can read more about Django’s asynchronous support, and how to best use async views, in [ Asynchronous support ](../async/) .
Django_meta/tutorialdjango_155_0.txt
6. Start the program (inside or outside of the debugger, using ⌃F5 (Windows, Linux Ctrl+F5 ) ), navigate to a /hello/name URL, and observe the results.
Django_meta/classbasedviews_20_0.txt
## Basic examples ¶
Django_meta/tutorialdjango_157_0.txt
## Serve static files
Django_meta/tutorialdjango_223_0.txt
# Replace the existing home function with the one below def home(request): return render(request, "hello/home.html")
Django_meta/tutorialdjango_188_0.txt
A base page template in Django contains all the shared parts of a set of pages, including references to CSS files, script files, and so forth. Base templates also define one or more **block** tags with content that extended templates are expected to override. A block tag is delineated by ` {% block <name> %} ` and ` {%...
Django_meta/classbasedviews_13_0.txt
* [ Getting Help ](https://docs.djangoproject.com/en/5.0/faq/help/)
Django_meta/modelspy_3_0.txt
class Genre(models.Model): """Model representing a book genre (e.g. Science Fiction, Non Fiction).""" name = models.CharField( max_length=200, unique=True, help_text="Enter a book genre (e.g. Science Fiction, French Poetry etc.)" )
Django_meta/django_meta_18_0.txt
### ` required_db_vendor ` ¶ ` Options. ` ` required_db_vendor ` ¶ Name of a supported database vendor that this model is specific to. Current built-in vendor names are: ` sqlite ` , ` postgresql ` , ` mysql ` , ` oracle ` . If this attribute is not empty and the current connection vendor doesn’t match ...
Django_meta/tutorialdjango_74_0.txt
from django.urls import path from hello import views
Django_meta/classbasedviews_66_0.txt
Try the FAQ — it's got answers to many common questions. [ Index ](/en/stable/genindex/) , [ Module Index ](/en/stable/py-modindex/) , or [ Table of Contents ](/en/stable/contents/)
Django_meta/tutorialdjango_46_0.txt
## Create and run a minimal Django app
Django_meta/tutorialdjango_12_0.txt
In this article Prerequisites Create a project environment for the Django tutorial Create and run a minimal Django app Create a debugger launch profile Explore the debugger Go to Definition and Peek Definition commands Use a template to render a page Serve static files Create multiple templates that extend a ba...
Django_meta/classbasedviews_24_0.txt
from django.urls import path from django.views.generic import TemplateView
Django_meta/tutorialdjango_248_0.txt
Because you changed your data models by editing ` models.py ` , you need to update the database itself. In VS Code, open a Terminal with your virtual environment activated (use the **Terminal: Create New Terminal** command, ⌃⇧` (Windows, Linux Ctrl+Shift+` ) )), navigate to the project folder, and run the following...
Django_meta/django_models_14_0.txt
## Getting help [ FAQ ](https://docs.djangoproject.com/en/3.2/faq/) Try the FAQ — it's got answers to many common questions. [ Index ](/en/stable/genindex/) , [ Module Index ](/en/stable/py-modindex/) , or [ Table of Contents ](/en/stable/contents/) Handy when looking for specific information. [ django-...
Django_meta/tutorialdjango_110_0.txt
3. Set a breakpoint at the first line of code in the ` hello_there ` function ( ` now = datetime.now() ` ) by doing any one of the following:
Django_meta/classbasedviews_83_0.txt
* [ GitHub ](https://github.com/django) * [ Twitter ](https://twitter.com/djangoproject) * [ Fediverse (Mastodon) ](https://fosstodon.org/@django) * [ News RSS ](https://www.djangoproject.com/rss/weblog/) * [ Django Users Mailing List ](https://groups.google.com/forum/#!forum/django-users)
Django_meta/modelspy_26_0.txt
class Author(models.Model): """Model representing an author.""" first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) date_of_birth = models.DateField(null=True, blank=True) date_of_death = models.DateField('died', null=True, blank=True)
Django_meta/django_models_12_0.txt
## Browse * Prev: [ Models and databases ](../) * Next: [ Making queries ](../queries/) * [ Table of contents ](https://docs.djangoproject.com/en/3.2/contents/) * [ General Index ](https://docs.djangoproject.com/en/3.2/genindex/) * [ Python Module Index ](https://docs.djangoproject.com/en/3.2/py-modindex/) ...
Django_meta/tutorialdjango_99_0.txt
path("hello/<name>", views.hello_there, name="hello_there"),
Django_meta/tutorialdjango_91_0.txt
![Django tutorial: start debugging/continue arrow on the debug toolbar](/assets/docs/python/django-tutorial/debug-continue-arrow.png)
Django_meta/introduction_11_0.txt
Django inherited Python’s “batteries-included” approach and includes out-of- the-box support for routine tasks in web development, including:
Django_meta/introduction_7_0.txt
_Django for Beginners_ is written for Django 4.2 and Python 3.11. All code examples work with these versions. By the time you read this, newer versions of both Django and Python may be available. In general, you should always strive to be on the latest version of Django and Python. As both are mature technologies, any ...
Django_meta/classbasedviews_47_0.txt
import asyncio from django.http import HttpResponse from django.views import View
Django_meta/classbasedviews_49_0.txt
Within a single view-class, all user-defined method handlers must be either synchronous, using ` def ` , or all asynchronous, using ` async def ` . An ` ImproperlyConfigured ` exception will be raised in ` as_view() ` if ` def ` and ` async def ` declarations are mixed.
Django_meta/introduction_5_0.txt
Django is a free, open-source web framework written in Python. First released in 2005, Django has been in continuous development since then and today powers many of the largest websites in the world, including Instagram, Pinterest, Bitbucket, and Disqus. At the same time, it is flexible enough to be a popular choice fo...
Django_meta/tutorialdjango_130_0.txt
10. Step through a few more lines of code, if you'd like, then select Continue ( F5 ) to let the program run. The browser window shows the result:
Django_meta/introduction_24_0.txt
Django is a mature web framework that strives to remain stable yet advance alongside the modern web. If you find yourself on a project with an older version of Django, there are [ detailed instructions ](https://docs.djangoproject.com/en/4.2/howto/upgrade-version/) for updating to the latest version.
Django_meta/classbasedviews_52_0.txt
[ Introduction to class-based views __ ](intro/)
Django_meta/tutorialdjango_315_0.txt
By default, Django provides an administrative interface for a web app that's protected by authentication. The interface is implemented through the built-in ` django.contrib.admin ` app, which is included by default in the project's ` INSTALLED_APPS ` list ( ` settings.py ` ), and authentication is handled with the buil...
Django_meta/django_models_4_1.txt
a select box instead of the standard text field and will limit choices to the choices given. A choices list looks like this: YEAR_IN_SCHOOL_CHOICES = [ ('FR', 'Freshman'), ('SO', 'Sophomore'), ('JR', 'Junior'), ('SR', 'Senior'), ('GR', 'Graduate'), ] No...
Django_meta/tutorialdjango_13_0.txt
[ Edit ](https://vscode.dev/github/microsoft/vscode- docs/blob/main/docs/python/tutorial-django.md "Edit this document in vscode.dev")
Django_meta/tutorialdjango_128_0.txt
9. Copy that line into the > prompt at the bottom of the debug console, and try changing the formatting:
Django_meta/modelspy_24_0.txt
def get_absolute_url(self): """Returns the url to access a particular book instance.""" return reverse('bookinstance-detail', args=[str(self.id)])
Django_meta/introduction_8_0.txt
You don’t need previous Python or web development experience to complete this book. Even someone new to programming and web development can follow along and feel the magic of writing web applications from scratch. This books is also suitable for programmers with previous web development experience but new to Django. Th...
Django_meta/django_meta_17_0.txt
### ` required_db_features ` ¶ ` Options. ` ` required_db_features ` ¶ List of database features that the current connection should have so that the model is considered during the migration phase. For example, if you set this list to ` ['gis_enabled'] ` , the model will only be synchronized on GIS- enabled...
Django_meta/tutorialdjango_338_0.txt
#### In this article there are 13 sections In this article
Django_meta/tutorialdjango_59_0.txt
Watching for file changes with StatReloader Performing system checks...
Django_meta/introduction_15_0.txt
Django remains [ under active development ](https://www.djangoproject.com/download/#supported-versions) with a regular release schedule of monthly security/bug fixes and a major new release every eight months. Millions of programmers have already used Django to build their websites. It doesn’t make sense to repeat the ...
Django_meta/tutorialdjango_178_0.txt
For production deployments, you typically collect all the static files from your apps into a single folder using the ` python manage.py collectstatic ` command. You can then use a dedicated static file server to serve those files, which typically results in better overall performance. The following steps show how this ...
Django_meta/tutorialdjango_264_0.txt
5. In ` views.py ` , define the view named ` log_message ` (as referred to by the URL route). This view handles both HTTP GET and POST cases. In the GET case (the ` else: ` section), it just displays the form that you defined in the previous steps. In the POST case, it retrieves the data from the form into a data obje...
Django_meta/classbasedviews_82_0.txt
## Follow Us
Django_meta/tutorialdjango_167_0.txt
1. In the ` hello ` folder, create a folder named ` static ` .
Django_meta/tutorialdjango_61_0.txt
Django's built-in web server is intended _only_ for local development purposes. When you deploy to a web host, however, Django uses the host's web server instead. The ` wsgi.py ` and ` asgi.py ` modules in the Django project take care of hooking into the production servers.
Django_meta/tutorialdjango_270_0.txt
7. Run the app and open a browser to the home page. Select the **Log Message** link on the nav bar, which should display the message logging page:
Django_meta/django_meta_27_0.txt
### ` label_lower ` ¶ ` Options. ` ` label_lower ` ¶ Representation of the model, returns ` app_label.model_name ` , e.g. ` 'polls.question' ` . [ __ Model class reference ](../class/) [ Model instance reference __ ](../instances/) __ Back to Top # Additional Information ## Support Django! ![Suppo...
Django_meta/django_models_4_7.txt
is defined. Then, refer to the other model class wherever needed. For example: from django.db import models from geography.models import ZipCode class Restaurant(models.Model): # ... zip_code = models.ForeignKey( ZipCode, on_delete=models.SET_NULL, ...
Django_meta/classbasedviews_11_0.txt
# [ Documentation ](https://docs.djangoproject.com/en/5.0/)
Django_meta/tutorialdjango_80_0.txt
6. In the VS Code Terminal, again with the virtual environment activated, run the development server with ` python manage.py runserver ` and open a browser to ` http://127.0.0.1:8000/ ` to see a page that renders "Hello, Django".
Django_meta/tutorialdjango_124_0.txt
![Django tutorial: local variables and arguments in VS Code during debugging](/assets/docs/python/django-tutorial/debug-local-variables.png)
Django_meta/introduction_53_0.txt
In the next chapter you’ll learn how to properly set up your computer and create your first Django project. Let’s begin!
Django_meta/modelspy_22_0.txt
status = models.CharField( max_length=1, choices=LOAN_STATUS, blank=True, default='d', help_text='Book availability')
Django_meta/tutorialdjango_63_0.txt
4. Ctrl+click the ` http://127.0.0.1:8000/ ` URL in the terminal output window to open your default browser to that address. If Django is installed correctly and the project is valid, you see the default page shown below. The VS Code terminal output window also shows the server log.
Django_meta/classbasedviews_53_0.txt
__ Back to Top
Django_meta/classbasedviews_9_0.txt
Toggle theme (current theme: dark)
Django_meta/django_models_17_0.txt
## Get Involved * [ Join a Group ](https://www.djangoproject.com/community/) * [ Contribute to Django ](https://docs.djangoproject.com/en/dev/internals/contributing/) * [ Submit a Bug ](https://docs.djangoproject.com/en/dev/internals/contributing/bugs-and-features/) * [ Report a Security Issue ](https://docs....
Django_meta/tutorialdjango_49_0.txt
### Create the Django project
Django_meta/classbasedviews_86_0.txt
[ Django ](https://www.djangoproject.com/)
Django_meta/django_models_8_3.txt
st_by "django.db.models.Options.get_latest_by") attribute, it will inherit these from its parent. If the parent has an ordering and you don’t want the child to have any natural ordering, you can explicitly disable it: class ChildModel(ParentModel): # ... class Meta: # Remove ...
Django_meta/classbasedviews_44_0.txt
If the view is accessed from a ` GET ` request, an object list is returned in the response (using the ` book_list.html ` template). But if the client issues a ` HEAD ` request, the response has an empty body and the ` Last- Modified ` header indicates when the most recent book was published. Based on this informat...
Django_meta/django_models_7_1.txt
if self.name == "Yoko Ono's blog": return # Yoko shall never have her own blog! else: super().save(*args, **kwargs) # Call the "real" save() method. It’s important to remember to call the superclass method – that’s that ` super().save(*args, **kwargs) ` business ...
Django_meta/tutorialdjango_280_0.txt
from django.views.generic import ListView
Django_meta/tutorialdjango_202_0.txt
Because the three pages you create in the next section extend ` layout.html ` , it saves time to create a **code snippet** to initialize a new template file with the appropriate reference to the base template. A code snippet provides a consistent piece of code from a single source, which avoids errors that can creep in...
Django_meta/tutorialdjango_55_0.txt
* ` __init__.py ` : an empty file that tells Python that this folder is a Python package. * ` asgi.py ` : an entry point for [ ASGI-compatible ](https://asgi.readthedocs.io/en/latest/) web servers to serve your project. You typically leave this file as-is as it provides the hooks for production web servers. * ` setting...
Django_meta/tutorialdjango_84_0.txt
1. Switch to **Run** view in VS Code (using the left-side activity bar or F5 ). You may see the message "To customize Run and Debug create a launch.json file". This means that you don't yet have a ` launch.json ` file containing debug configurations. VS Code can create that for you if you click on the **create a laun...
Django_meta/tutorialdjango_294_0.txt
## Use the debugger with page templates
Django_meta/tutorialdjango_126_0.txt
now.strftime("%A, %d %B, %Y at %X") 'Friday, 07 September, 2018 at 07:46:32'
Django_meta/tutorialdjango_302_0.txt
4. You can also work with variables in the **Debug Console** panel. (Django filters like ` date ` , however, are not presently available in the console.)
Django_meta/django_models_9_0.txt
## Organizing models in a package ¶ The [ ` manage.py startapp ` ](../../../ref/django-admin/#django-admin- startapp) command creates an application structure that includes a ` models.py ` file. If you have many models, organizing them in separate files may be useful. To do so, create a ` models ` package. Remov...
Django_meta/introduction_48_0.txt
The success of Django owes as much to its community as it does the technological achievement of the framework itself. “Come for the framework, stay for the community” is a common saying among Django developers. It extends to the technical development of Django, which happens online via the [ django- developers ](https:...
Django_meta/tutorialdjango_45_0.txt
You now have a self-contained environment ready for writing Django code. VS Code activates the environment automatically when you use [ **Terminal: Create New Terminal** ](/docs/terminal/basics) ( ⌃⇧` (Windows, Linux Ctrl+Shift+` ) ). If you open a separate command prompt or terminal, activate the environment by r...
Django_meta/classbasedviews_70_0.txt
Join the Django Discord Community. [ Official Django Forum ](https://forum.djangoproject.com/)
Django_meta/tutorialdjango_58_0.txt
3. To verify the Django project, make sure your virtual environment is activated, then start Django's development server using the command ` python manage.py runserver ` . The server runs on the default port 8000, and you see output like the following output in the terminal window:
Django_meta/tutorialdjango_33_0.txt
4. In VS Code, open the Command Palette ( **View** > **Command Palette** or ( ⇧⌘P (Windows, Linux Ctrl+Shift+P ) )). Then select the **Python: Select Interpreter** command:
Django_meta/tutorialdjango_35_0.txt
5. The command presents a list of available interpreters that VS Code can locate automatically (your list will vary; if you don't see the desired interpreter, see [ Configuring Python environments ](/docs/python/environments) ). From the list, select the virtual environment in your project folder that starts with ` ./....
Django_meta/tutorialdjango_109_0.txt
The ` name ` variable defined in the URL route is given as an argument to the ` hello_there ` function. As described in the code comments, always filter arbitrary user-provided information to avoid various attacks on your app. In this case, the code filters the name argument to contain only letters, which avoids injec...
Django_meta/classbasedviews_1_0.txt
The web framework for perfectionists with deadlines.
Django_meta/classbasedviews_31_0.txt
Then we need to add this new view into our URLconf. [ ` TemplateView ` ](../../ref/class-based-views/base/#django.views.generic.base.TemplateView "django.views.generic.base.TemplateView") is a class, not a function, so we point the URL to the [ ` as_view() ` ](../../ref/class-based- views/base/#django.views.generic....
Django_meta/tutorialdjango_54_0.txt
* A subfolder named ` web_project ` , which contains the following files:
Django_meta/tutorialdjango_67_0.txt
1. In the VS Code Terminal with your virtual environment activated, run the administrative utility's ` startapp ` command in your project folder (where ` manage.py ` resides):
Django_meta/tutorialdjango_15_0.txt
Django is a high-level Python framework designed for rapid, secure, and scalable web development. Django includes rich support for URL routing, page templates, and working with data.
Django_meta/django_models_8_4.txt
to add a method to the ` Person ` model. You can do it like this: from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) class MyPerson(Person): class Meta: pr...
Django_meta/django_models_4_6.txt
ing the attributes of the many-to-many-related model: # Find all the groups with a member whose name starts with 'Paul' >>> Group.objects.filter(members__name__startswith='Paul') <QuerySet [<Group: The Beatles>]> As you are using an intermediate model, you can also query on its attributes: ...
Django_meta/tutorialdjango_64_0.txt
![Django tutorial: default view of empty Django project](/assets/docs/python/django-tutorial/django-empty-project-success.png)
Django_meta/tutorialdjango_105_0.txt
def hello_there(request, name): now = datetime.now() formatted_now = now.strftime("%A, %d %B, %Y at %X")
Django_meta/classbasedviews_48_0.txt
class AsyncView(View): async def get(self, request, *args, **kwargs): # Perform io-blocking view logic using await, sleep for example. await asyncio.sleep(1) return HttpResponse("Hello async world!")
Django_meta/tutorialdjango_148_0.txt
3. In the ` templates/hello ` folder, create a file named ` hello_there.html ` with the contents below. This template contains two placeholders for data values named "name", and "date", which are delineated by pairs of curly braces, ` {{ ` and ` }} ` . All other invariant text is part of the template, along with format...
Django_meta/django_models_4_0.txt
## Fields ¶ The most important part of a model – and the only required part of a model – is the list of database fields it defines. Fields are specified by class attributes. Be careful not to choose field names that conflict with the [ models API ](../../../ref/models/instances/) like ` clean ` , ` save ` , or ` ...
Django_meta/tutorialdjango_168_0.txt
2. Within the ` static ` folder, create a subfolder named ` hello ` , matching the app name.
Django_meta/introduction_49_0.txt
Regardless of your level of technical expertise, becoming involved in Django is a great way to learn, meet other developers, and enhance your reputation.
Django_meta/introduction_4_0.txt
Welcome to _Django for Beginners_ , a project-based approach to learning web development with the [ Django ](https://djangoproject.com) web framework. In this book, you will build and deploy five progressively more complex web applications, starting with a simple _Hello, World_ app, progressing to a _Pages_ app, a _Mes...