id
stringlengths
16
145
text
stringlengths
1
179k
title
stringclasses
1 value
Django_meta/classbasedviews_38_0.txt
from django.urls import path from books.views import BookListView
Django_meta/django_models_4_3.txt
del. [ ` ForeignKey ` ](../../../ref/models/fields/#django.db.models.ForeignKey "django.db.models.ForeignKey") requires a positional argument: the class to which the model is related. For example, if a ` Car ` model has a ` Manufacturer ` – that is, a ` Manufacturer ` makes multiple cars but each ` Car ` only ha...
Django_meta/tutorialdjango_295_0.txt
As shown in the previous section, page templates can contain procedural directives like ` {% for message in message_list %} ` and ` {% if message_list %} ` , rather than only passive, declarative elements like ` {% url %} ` and ` {% block %} ` . As a result, you can have programming errors inside templates as with any ...
Django_meta/tutorialdjango_172_0.txt
4. In ` templates/hello/hello_there.html ` , add the following lines after the ` <title> ` element. The ` {% load static %} ` tag is a custom Django template tag set, which allows you to use ` {% static %} ` to refer to a file like the stylesheet.
Django_meta/tutorialdjango_212_0.txt
With the code snippet in place, you can quickly create templates for the Home, About, and Contact pages.
Django_meta/tutorialdjango_204_0.txt
2. In the list that appears, select **html** . (The option may appear as "html.json" in the **Existing Snippets** section of the list if you've created snippets previously.)
Django_meta/classbasedviews_10_0.txt
Toggle Light / Dark / Auto color theme
Django_meta/tutorialdjango_229_0.txt
## Work with data, data models, and migrations
Django_meta/introduction_12_0.txt
* User authentication * Testing * Database models, forms, URL routes, and templates * Admin interface * Security and performance upgrades * Support for multiple database backends
Django_meta/tutorialdjango_230_0.txt
Many web apps work with information stored in a database, and Django makes it easy to represent the objects in that database using _models_ . In Django, a model is a Python class, derived from ` django.db.models.Model ` , that represents a specific database object, typically a table. You place these classes in an app's...
Django_meta/tutorialdjango_26_0.txt
1. On your file system, create a project folder for this tutorial, such as ` hello_django ` .
Django_meta/classbasedviews_0_0.txt
[ Django ](https://www.djangoproject.com/)
Django_meta/tutorialdjango_165_0.txt
urlpatterns += staticfiles_urlpatterns()
Django_meta/tutorialdjango_108_0.txt
content = "Hello there, " + clean_name + "! It's " + formatted_now return HttpResponse(content)
Django_meta/classbasedviews_36_0.txt
Suppose somebody wants to access our book library over HTTP using the views as an API. The API client would connect every now and then and download book data for the books published since last visit. But if no new books appeared since then, it is a waste of CPU time and bandwidth to fetch the books from the database, r...
Django_meta/tutorialdjango_169_0.txt
The reason for this extra subfolder is that when you deploy the Django project to a production server, you collect all the static files into a single folder that's then served by a dedicated static file server. The ` static/hello ` subfolder ensures that when the app's static files are collected, they're in an app-spec...
Django_meta/modelspy_10_0.txt
class Meta: constraints = [ UniqueConstraint( Lower('name'), name='language_name_case_insensitive_unique', violation_error_message = "Language already exists (case insensitive match)" ), ]
Django_meta/classbasedviews_4_0.txt
Toggle theme (current theme: light)
Django_meta/tutorialdjango_236_0.txt
### Types of databases
Django_meta/tutorialdjango_255_0.txt
from django import forms from hello.models import LogMessage
Django_meta/modelspy_14_0.txt
display_genre.short_description = 'Genre'
Django_meta/tutorialdjango_76_0.txt
4. The ` web_project ` folder also contains a ` urls.py ` file, which is where URL routing is actually handled. Open ` web_project/urls.py ` and modify it to match the following code (you can retain the instructive comments if you like). This code pulls in the app's ` hello/urls.py ` using ` django.urls.include ` , wh...
Django_meta/tutorialdjango_1_0.txt
* [ Docs ](/docs) * [ Updates ](/updates) * [ Blog ](/blogs) * [ API ](/api) * [ Extensions ](https://marketplace.visualstudio.com/VSCode) * [ FAQ ](/docs/supporting/faq) * [ Learn ](/learn) * [ Search ](/Search)
Django_meta/tutorialdjango_140_0.txt
## Use a template to render a page
Django_meta/tutorialdjango_225_0.txt
def contact(request): return render(request, "hello/contact.html")
Django_meta/tutorialdjango_138_0.txt
* **Peek Definition** ( ⌥F12 (Windows Alt+F12 , Linux Ctrl+Shift+F10 ) , also on the right-click context menu), is similar, but displays the class definition directly in the editor (making space in the editor window to avoid obscuring any code). Press Escape to close the Peek window or use the **x** in the upp...
Django_meta/tutorialdjango_163_0.txt
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
Django_meta/tutorialdjango_103_0.txt
import re from django.utils.timezone import datetime from django.http import HttpResponse
Django_meta/tutorialdjango_261_0.txt
input[name=message] { width: 80%; }
Django_meta/introduction_2_0.txt
# Introduction
Django_meta/tutorialdjango_283_0.txt
class HomeListView(ListView): """Renders the home page, with a list of all messages.""" model = LogMessage
Django_meta/django_models_1_0.txt
[ Django ](https://www.djangoproject.com/) The web framework for perfectionists with deadlines. __ Menu Toggle theme (current theme: auto) Toggle theme (current theme: light) Toggle theme (current theme: dark) Toggle Light / Dark / Auto color theme * [ Overview ](https://www.djangoproject.com/start/overview/) ...
Django_meta/tutorialdjango_79_0.txt
5. Save all modified files.
Django_meta/tutorialdjango_23_0.txt
* (All operating systems) A download from [ python.org ](https://www.python.org/downloads/) ; typically use the **Download Python 3.9.1** button that appears first on the page (or whatever is the latest version). * (Linux) The built-in Python 3 installation works well, but to install other Python packages you must run ...
Django_meta/tutorialdjango_330_0.txt
## Next steps
Django_meta/tutorialdjango_21_0.txt
1. Install the [ Python extension ](https://marketplace.visualstudio.com/items?itemName=ms-python.python) .
Django_meta/tutorialdjango_250_0.txt
Take a look in the ` migrations ` folder to see the scripts that ` makemigrations ` generates. You can also look at the database itself to see that the schema is updated.
Django_meta/modelspy_11_0.txt
class Book(models.Model): """Model representing a book (but not a specific copy of a book).""" title = models.CharField(max_length=200) author = models.ForeignKey('Author', on_delete=models.RESTRICT, null=True) # Foreign Key used because book can only have one author, but authors can have multiple books. # Author as a ...
Django_meta/introduction_47_0.txt
## Community
Django_meta/django_models_8_6.txt
ract base classes. Overriding an inherited field which is referenced by an inherited [ ` Manager ` ](../managers/#django.db.models.Manager "django.db.models.Manager") may cause subtle bugs. See [ custom managers and model inheritance ](../managers/#custom-managers-and-inheritance) . Note Some fields define extra att...
Django_meta/tutorialdjango_29_0.txt
# macOS python3 -m venv .venv source .venv/bin/activate
Django_meta/classbasedviews_19_0.txt
* [ Introduction to class-based views ](intro/) * [ Built-in class-based generic views ](generic-display/) * [ Form handling with class-based views ](generic-editing/) * [ Using mixins with class-based views ](mixins/)
Django_meta/tutorialdjango_304_0.txt
## Optional activities
Django_meta/tutorialdjango_160_0.txt
When switching to production, navigate to ` settings.py ` , set ` DEBUG=False ` , and change ` ALLOWED_HOSTS = ['*'] ` to allow specific hosts. This may result in additional work when using containers. For details, see [ Issue 13 ](https://github.com/microsoft/python-sample-vscode-django-tutorial/issues/13) .
Django_meta/tutorialdjango_201_0.txt
### Create a code snippet
Django_meta/tutorialdjango_331_0.txt
Congratulations on completing this walkthrough of working with Django in Visual Studio Code!
Django_meta/tutorialdjango_266_0.txt
# Add this code elsewhere in the file: def log_message(request): form = LogMessageForm(request.POST or None)
Django_meta/modelspy_0_0.txt
from django.db import models
Django_meta/tutorialdjango_256_0.txt
class LogMessageForm(forms.ModelForm): class Meta: model = LogMessage fields = ("message",) # NOTE: the trailing comma is required
Django_meta/tutorialdjango_271_0.txt
![Django tutorial: the message logging page added to the app](/assets/docs/python/django-tutorial/message-logging-page.png)
Django_meta/tutorialdjango_196_0.txt
.navbar a { text-decoration: none; color: inherit; }
Django_meta/tutorialdjango_193_0.txt
<div class="body-content"> {% block content %} {% endblock %} <hr/> <footer> <p>&copy; 2018</p> </footer> </div> </body> </html>
Django_meta/tutorialdjango_9_0.txt
* C# * [ Intro Videos ](/docs/csharp/introvideos-csharp) * [ Get Started ](/docs/csharp/get-started) * [ Navigate and Edit ](/docs/csharp/navigate-edit) * [ IntelliCode ](/docs/csharp/intellicode) * [ Refactoring ](/docs/csharp/refactoring) * [ Formatting and Linting ](/docs/csharp/formatting-linting) * [ Project Manag...
Django_meta/classbasedviews_71_0.txt
Join the community on the Django Forum. [ Ticket tracker ](https://code.djangoproject.com/)
Django_meta/django_models_20_0.txt
## Support Us * [ Sponsor Django ](https://www.djangoproject.com/fundraising/) * [ Corporate membership ](/foundation/corporate-membership/) * [ Official merchandise store ](https://django.threadless.com/) * [ Benevity Workplace Giving Program ](/foundation/donate/#benevity-giving) [ Django ](https://www.dja...
Django_meta/tutorialdjango_96_0.txt
Debugging gives you the opportunity to pause a running program on a particular line of code. When a program is paused, you can examine variables, run code in the Debug Console panel, and otherwise take advantage of the features described on [ Debugging ](/docs/python/debugging) . Running the debugger also automatically...
Django_meta/tutorialdjango_139_0.txt
![Django tutorial: Peek Definition showing the Flask class inline](/assets/docs/python/django-tutorial/peek-definition.png)
Django_meta/tutorialdjango_156_0.txt
7. Also try navigating to a /hello/name URL using a name like ` <a%20value%20that%20could%20be%20HTML> ` to see Django's automatic escaping at work. The "name" value shows up as plain text in the browser rather than as rendering an actual element.
Django_meta/tutorialdjango_238_0.txt
For these reasons, consider using a production-level data store such as [ PostgreSQL ](https://www.postgresql.org/) , [ MySQL ](https://www.mysql.com/) , and [ SQL Server ](https://www.microsoft.com/en-ca/sql-server/) . For information on Django's support for other databases, see [ Database setup ](https://docs.djangop...
Django_meta/django_models_4_5.txt
estriction applies to the foreign key to the target model (this would be ` Person ` in our example). * For a model which has a many-to-many relationship to itself through an intermediary model, two foreign keys to the same model are permitted, but they will be treated as the two (different) sides of the many-to-man...
Django_meta/tutorialdjango_191_0.txt
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>{% block title %}{% endblock %}</title> {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'hello/site.css' %}"/> </head>
Django_meta/django_models_3_0.txt
## Using models ¶ Once you have defined your models, you need to tell Django you’re going to _use_ those models. Do this by editing your settings file and changing the [ ` INSTALLED_APPS ` ](../../../ref/settings/#std-setting-INSTALLED_APPS) setting to add the name of the module that contains your ` models.py ` . ...
Django_meta/introduction_0_0.txt
[ Django for Beginners ](/)
Django_meta/tutorialdjango_220_0.txt
5. In the app's ` urls.py ` , add routes for the /about and /contact pages. Be mindful that the ` name ` argument to the ` path ` function defines the name with which you refer to the page in the ` {% url %} ` tags in the templates.
Django_meta/tutorialdjango_237_0.txt
By default, Django includes a ` db.sqlite3 ` file for an app's database that's suitable for development work. As described on [ When to use SQLite ](https://www.sqlite.org/whentouse.html) (sqlite.org), SQLite works fine for low to medium traffic sites with fewer than 100 K hits/day, but is not recommended for higher vo...
Django_meta/tutorialdjango_170_0.txt
3. In the ` static/hello ` folder, create a file named ` site.css ` with the following contents. After entering this code, also observe the syntax highlighting that VS Code provides for CSS files, including a color preview.
Django_meta/classbasedviews_42_0.txt
class BookListView(ListView): model = Book
Django_meta/tutorialdjango_213_0.txt
1. In the ` templates/hello ` folder, create a new file named ` home.html ` , Then start typing ` djext ` to see the snippet appear as a completion:
Django_meta/tutorialdjango_296_0.txt
Fortunately, the Python Extension for VS Code provides template debugging when you have ` "django": true ` in the debugging configuration (as you do already). The following steps demonstrate this capability:
Django_meta/classbasedviews_80_0.txt
## Get Help
Django_meta/classbasedviews_26_0.txt
Any arguments passed to [ ` as_view() ` ](../../ref/class-based- views/base/#django.views.generic.base.View.as_view "django.views.generic.base.View.as_view") will override attributes set on the class. In this example, we set ` template_name ` on the ` TemplateView ` . A similar overriding pattern can be used for th...
Django_meta/tutorialdjango_123_0.txt
7. On the left side of the VS Code window, you see a **Variables** pane that shows local variables, such as ` now ` , as well as arguments, such as ` name ` . Below that are panes for **Watch** , **Call Stack** , and **Breakpoints** (see [ VS Code debugging ](/docs/editor/debugging) for details). In the **Locals** sect...
Django_meta/introduction_39_0.txt
def make_my_website: ... print("All done!") # new
Django_meta/tutorialdjango_86_0.txt
2. Select the link and VS Code will prompt for a debug configuration. Select **Django** from the dropdown and VS Code will populate a new ` launch.json ` file with a Django run configuration. The ` launch.json ` file contains a number of debugging configurations, each of which is a separate JSON object within the ` con...
Django_meta/tutorialdjango_245_0.txt
def __str__(self): """Returns a string representation of a message.""" date = timezone.localtime(self.log_date) return f"'{self.message}' logged on {date.strftime('%A, %d %B, %Y at %X')}"
Django_meta/django_meta_5_0.txt
### ` db_table ` ¶ ` Options. ` ` db_table ` ¶ The name of the database table to use for the model: db_table = 'music_album'
Django_meta/tutorialdjango_298_0.txt
![Django tutorial: breakpoints set in a Django page template](/assets/docs/python/django-tutorial/template-breakpoints.png)
Django_meta/tutorialdjango_278_0.txt
.message_list th,td { text-align: left; padding-right: 15px; }
Django_meta/tutorialdjango_240_0.txt
A Django model is again a Python class derived from ` django.db.model.Models ` , which you place in the app's ` models.py ` file. In the database, each model is automatically given a unique ID field named ` id ` . All other fields are defined as properties of the class using types from ` django.db.models ` such as ` Ch...
Django_meta/django_models_8_2.txt
./ref/django-admin/#django-admin-migrate) ). If you don’t specify a [ ` related_name ` ](../../../ref/models/fields/#django.db.models.ForeignKey.related_name "django.db.models.ForeignKey.related_name") attribute for a field in an abstract base class, the default reverse name will be the name of the child class follow...
Django_meta/tutorialdjango_119_0.txt
![Django tutorial: the VS Code debug toolbar](/assets/docs/python/shared/debug-toolbar.png)
Django_meta/tutorialdjango_0_0.txt
Skip to content __ [ Visual Studio Code ](/)
Django_meta/tutorialdjango_232_0.txt
1. Make changes to the models in your ` models.py ` file. 2. Run ` python manage.py makemigrations ` to generate scripts in the ` migrations ` folder that migrate the database from its current state to the new state. 3. Run ` python manage.py migrate ` to apply the scripts to the actual database.
Django_meta/introduction_43_0.txt
You can only trust some of what you read online, of course, and with experience, you will develop the context to see how the pieces of Django and code fit together.
Django_meta/tutorialdjango_85_0.txt
![Django tutorial: initial view of the debug panel](/assets/docs/python/shared/debug-panel-initial-view.png)
Django_meta/tutorialdjango_171_0.txt
.message { font-weight: 600; color: blue; }
Django_meta/modelspy_9_0.txt
def __str__(self): """String for representing the Model object (in Admin site etc.)""" return self.name
Django_meta/classbasedviews_33_0.txt
urlpatterns = [ path("about/", AboutView.as_view()), ]
Django_meta/tutorialdjango_184_0.txt
Because most web apps have more than one page, and because those pages typically share many common elements, developers separate those common elements into a base page template that other page templates then extend. (This is also called template inheritance, meaning the extended pages inherit elements from the base pag...
Django_meta/tutorialdjango_114_0.txt
4. Start the debugger by selecting the **Run** > **Start Debugging** menu command, or selecting the green **Start Debugging** arrow next to the list ( F5 ):
Django_meta/classbasedviews_6_0.txt
Toggle Light / Dark / Auto color theme
Django_meta/tutorialdjango_57_0.txt
When you run the server the first time, it creates a default SQLite database in the file ` db.sqlite3 ` that is intended for development purposes, but can be used in production for low-volume web apps. For additional information about databases, see the Types of databases section.
Django_meta/tutorialdjango_43_0.txt
9. Install Django in the virtual environment by running the following command in the VS Code Terminal:
Django_meta/tutorialdjango_101_0.txt
URL routes are case-sensitive. For example, the route ` /hello/<name> ` is distinct from ` /Hello/<name> ` . If you want the same view function to handle both, define paths for each variant.
Django_meta/introduction_28_0.txt
In **Chapter 3** , we make, test, and deploy a _Pages_ app that introduces templates and class-based views. Templates are how Django allows for DRY (Don’t Repeat Yourself) development with HTML and CSS, while class-based views are quite powerful yet require minimal code. We also add our first tests and deploy the websi...
Django_meta/classbasedviews_67_0.txt
Handy when looking for specific information. [ django-users mailing list ](https://groups.google.com/group/django-users/)
Django_meta/tutorialdjango_2_0.txt
* [ ![Search](/assets/icons/search.svg) ![Search](/assets/icons/search_dark.svg) ](/Search "Search") * ![Search](/assets/icons/search.svg) ![Search](/assets/icons/search_dark.svg)
Django_meta/tutorialdjango_282_0.txt
# Remove the old home function if you want; it's no longer used
Django_meta/tutorialdjango_292_0.txt
![Django tutorial: app home page displaying message from the database](/assets/docs/python/django-tutorial/app-with-message-list.png)
Django_meta/tutorialdjango_143_0.txt
In Django, a template is an HTML file that contains placeholders for values that the code provides at run time. The Django templating engine then takes care of making the substitutions when rendering the page, and provides automatic escaping to prevent XSS attacks (that is, if you tried using HTML in a data value, you ...