id
stringlengths
16
145
text
stringlengths
1
179k
title
stringclasses
1 value
Django_meta/tutorialdjango_284_0.txt
def get_context_data(self, **kwargs): context = super(HomeListView, self).get_context_data(**kwargs) return context
Django_meta/tutorialdjango_231_0.txt
With Django, you work with your database almost exclusively through the models you define in code. Django's "migrations" then handle all the details of the underlying database automatically as you evolve the models over time. The general workflow is as follows:
Django_meta/classbasedviews_7_0.txt
* [ Overview ](https://www.djangoproject.com/start/overview/) * [ Download ](https://www.djangoproject.com/download/) * [ Documentation ](https://docs.djangoproject.com/) * [ News ](https://www.djangoproject.com/weblog/) * [ Community ](https://www.djangoproject.com/community/) * [ Code ](https://github.com/django/djan...
Django_meta/modelspy_1_0.txt
# Create your models here.
Django_meta/tutorialdjango_247_0.txt
### Migrate the database
Django_meta/tutorialdjango_166_0.txt
### Refer to static files in a template
Django_meta/introduction_35_0.txt
## Book Layout
Django_meta/tutorialdjango_279_0.txt
12. In ` views.py ` , import Django's generic ` ListView ` class, which we'll use to implement the home page:
Django_meta/tutorialdjango_318_0.txt
Be sure to remember your username and password combination. These are the credentials you use to authenticate with the app.
Django_meta/tutorialdjango_135_0.txt
## Go to Definition and Peek Definition commands
Django_meta/classbasedviews_59_0.txt
* Class-based views * Basic examples * Usage in your URLconf * Subclassing generic views * Supporting other HTTP methods * Asynchronous class-based views
Django_meta/tutorialdjango_324_0.txt
5. Once you're authenticated, you see the default administration page, through which you can manage users and groups:
Django_meta/tutorialdjango_30_0.txt
# Windows py -3 -m venv .venv .venv\scripts\activate
Django_meta/classbasedviews_39_0.txt
urlpatterns = [ path("books/", BookListView.as_view()), ]
Django_meta/tutorialdjango_249_0.txt
python manage.py makemigrations python manage.py migrate
Django_meta/classbasedviews_64_0.txt
## Getting help
Django_meta/introduction_46_0.txt
There are two fantastic online resources where the Django community gathers to ask and answer questions. The first is the [ official Django Forum ](https://forum.djangoproject.com/) , and the second is the [ Django Users Google Group ](https://groups.google.com/g/django-users) . Each is an excellent next step if you ne...
Django_meta/tutorialdjango_122_0.txt
6. Use Step Over to run the ` now = datetime.now() ` statement.
Django_meta/tutorialdjango_87_0.txt
3. Scroll down to and examine the configuration with the name "Python: Django":
Django_meta/tutorialdjango_161_0.txt
### Ready the app for static files
Django_meta/modelspy_20_0.txt
@property def is_overdue(self): """Determines if the book is overdue based on due date and current date.""" return bool(self.due_back and date.today() > self.due_back)
Django_meta/tutorialdjango_131_0.txt
![Django tutorial: result of the modified program](/assets/docs/python/django- tutorial/debug-run-result.png)
Django_meta/tutorialdjango_98_0.txt
1. In ` hello/urls.py ` , add a route to the ` urlpatterns ` list:
Django_meta/django_meta_9_0.txt
### ` default_related_name ` ¶ ` Options. ` ` default_related_name ` ¶ The name that will be used by default for the relation from a related object back to this one. The default is ` <model_name>_set ` . This option also sets [ ` related_query_name ` ](../fields/#django.db.models.ForeignKey.related_query...
Django_meta/introduction_37_0.txt
# This is Python code print("Hello, World!")
Django_meta/tutorialdjango_83_0.txt
You're probably already wondering if there's an easier way to run the server and test the app without typing ` python manage.py runserver ` each time. Fortunately, there is! You can create a customized launch profile in VS Code, which is also used for the inevitable exercise of debugging.
Django_meta/classbasedviews_77_0.txt
* [ About Django ](https://www.djangoproject.com/start/overview/) * [ Getting Started with Django ](https://www.djangoproject.com/start/) * [ Team Organization ](https://docs.djangoproject.com/en/dev/internals/organization/) * [ Django Software Foundation ](https://www.djangoproject.com/foundation/) * [ Code of Conduct...
Django_meta/introduction_56_0.txt
Copyright © William Vincent
Django_meta/tutorialdjango_211_0.txt
### Use the code snippet to add pages
Django_meta/tutorialdjango_127_0.txt
> **Tip** : The **Debug Console** also shows exceptions from within the app > that may not appear in the terminal. For example, if you see a "Paused on > exception" message in the **Call Stack** area of **Run and Debug** view, > switch to the **Debug Console** to see the exception message.
Django_meta/tutorialdjango_104_0.txt
def home(request): return HttpResponse("Hello, Django!")
Django_meta/django_models_15_0.txt
## Download: Offline (Django 3.2): [ HTML ](https://media.djangoproject.com/docs/django-docs-3.2-en.zip) | [ PDF ](https://media.readthedocs.org/pdf/django/3.2.x/django.pdf) | [ ePub ](https://media.readthedocs.org/epub/django/3.2.x/django.epub) Provided by [ Read the Docs ](https://readthedocs.org/) . # Django ...
Django_meta/modelspy_19_0.txt
class BookInstance(models.Model): """Model representing a specific copy of a book (i.e. that can be borrowed from the library).""" id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text="Unique ID for this particular book across whole library") book = models.ForeignKey('Book', on_delete=models.RESTRICT,...
Django_meta/tutorialdjango_95_0.txt
## Explore the debugger
Django_meta/classbasedviews_17_0.txt
# Class-based views ¶
Django_meta/tutorialdjango_206_0.txt
"Django Tutorial: template extending layout.html": { "prefix": "djextlayout", "body": [ "{% extends \"hello/layout.html\" %}", "{% block title %}", "$0", "{% endblock %}", "{% block content %}", "{% endblock %}" ],
Django_meta/django_meta_15_0.txt
### ` default_permissions ` ¶ ` Options. ` ` default_permissions ` ¶ Defaults to ` ('add', 'change', 'delete', 'view') ` . You may customize this list, for example, by setting this to an empty list if your app doesn’t require any of the default permissions. It must be specified on the model before the m...
Django_meta/tutorialdjango_242_0.txt
For example, add the following class in ` models.py ` to define a data model that represents dated entries in a simple message log:
Django_meta/tutorialdjango_179_0.txt
1. In ` web_project/settings.py ` , add the following line that defines a location where static files are collected when you use the ` collectstatic ` command:
Django_meta/introduction_23_0.txt
Django 4.2 is an LTS (Long Term Support) release meaning it will receive security updates for at least three years after its release. The previous LTS was Django 3.2, which ends support in April 2024. As with all major releases, there are many new improvements for the 4.2 release, including a light or dark color theme ...
Django_meta/tutorialdjango_89_0.txt
This configuration tells VS Code to run ` "${workspaceFolder}/manage.py" ` using the selected Python interpreter and the arguments in the ` args ` list. Launching the VS Code debugger with this configuration, then, is the same as running ` python manage.py runserver ` in the VS Code Terminal with your activated virtua...
Django_meta/tutorialdjango_19_0.txt
## Prerequisites
Django_meta/introduction_42_0.txt
The good news is that whatever error you are having, it is likely that you are not the first! Copy and paste your error into a search engine like Google or DuckDuckGo; it will typically bring up something from StackOverflow or a personal blog detailing the same issue. Experienced programmers often joke that their abili...
Django_meta/tutorialdjango_8_0.txt
* [ Debugging ](/docs/typescript/typescript-debugging) * Python * [ Quick Start ](/docs/python/python-quick-start) * [ Tutorial ](/docs/python/python-tutorial) * [ Editing Code ](/docs/python/editing) * [ Linting ](/docs/python/linting) * [ Formatting ](/docs/python/formatting) * [ Debugging ](/docs/python/debugging) *...
Django_meta/classbasedviews_81_0.txt
* [ Getting Help FAQ ](https://docs.djangoproject.com/en/stable/faq/) * [ #django IRC channel ](irc://irc.libera.chat/django) * [ Django Discord ](https://discord.gg/xcRH6mN4fa) * [ Official Django Forum ](https://forum.djangoproject.com/)
Django_meta/tutorialdjango_132_0.txt
11. Change the line in the code to use different datetime format, for example ` now.strftime("%a, %d %b, %y at %X") ` , and then save the file. The Django server will automatically reload, which means the changes will be applied without the need to restart the debugger. Refresh the page on the browser to see the update...
Django_meta/classbasedviews_34_0.txt
For more information on how to use the built in generic views, consult the next topic on [ generic class-based views ](generic-display/) .
Django_meta/classbasedviews_76_0.txt
## Learn More
Django_meta/tutorialdjango_329_0.txt
* Create a ` Dockerfile ` file describing a simple Python container. * Build, run, and verify the functionality of a [ Django ](https://www.djangoproject.com/) app. * Debug the app running in a container.
Django_meta/tutorialdjango_273_0.txt
9. Stop the app when you're done.
Django_meta/classbasedviews_51_0.txt
[ __ Templates ](../templates/)
Django_meta/classbasedviews_75_0.txt
# Django Links
Django_meta/tutorialdjango_31_0.txt
> **Note** : Use a stock Python installation when running the above commands. > If you use ` python.exe ` from an Anaconda installation, you see an error > because the ensurepip module isn't available, and the environment is left in > an unfinished state.
Django_meta/classbasedviews_16_0.txt
* __
Django_meta/tutorialdjango_265_0.txt
# Add these to existing imports at the top of the file: from django.shortcuts import redirect from hello.forms import LogMessageForm from hello.models import LogMessage
Django_meta/tutorialdjango_246_0.txt
A model class can include methods that return values computed from other class properties. Models typically include a ` __str__ ` method that returns a string representation of the instance.
Django_meta/django_meta_27_1.txt
t.com/en/dev/internals/security/#reporting-security-issues) ## Get Help * [ Getting Help FAQ ](https://docs.djangoproject.com/en/stable/faq/) * [ #django IRC channel ](irc://irc.libera.chat/django) * [ Django Discord ](https://discord.gg/xcRH6mN4fa) * [ Official Django Forum ](https://forum.djangoproject.com...
Django_meta/django_meta_20_0.txt
### ` indexes ` ¶ ` Options. ` ` indexes ` ¶ A list of [ indexes ](../indexes/) that you want to define on the model: from django.db import models class Customer(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100)...
Django_meta/tutorialdjango_39_0.txt
7. The selected environment appears on the right side of the VS Code status bar, and notices the **('.venv': venv)** indicator that tells you that you're using a virtual environment:
Django_meta/tutorialdjango_200_0.txt
You can run the app at this point, but because you haven't made use of the base template anywhere and haven't changed any code files, the result is the same as the previous step. Complete the remaining sections to see the final effect.
Django_meta/tutorialdjango_180_0.txt
STATIC_ROOT = BASE_DIR / 'static_collected'
Django_meta/tutorialdjango_293_0.txt
18. Stop the app when you're done.
Django_meta/django_models_8_1.txt
Due to the way Python inheritance works, if a child class inherits from multiple abstract base classes, only the Meta options from the first listed class will be inherited by default. To inherit Meta options from multiple abstract base classes, you must explicitly declare the Meta inheritance. For example: ...
Django_meta/modelspy_16_0.txt
def __str__(self): """String for representing the Model object.""" return self.title
Django_meta/tutorialdjango_244_0.txt
class LogMessage(models.Model): message = models.CharField(max_length=300) log_date = models.DateTimeField("date logged")
Django_meta/tutorialdjango_36_0.txt
![Django tutorial: Selecting the virtual environment for Python](/assets/docs/python/shared/select-virtual-environment.png)
Django_meta/tutorialdjango_50_0.txt
1. In the VS Code Terminal where your virtual environment is activated, run the following command:
Django_meta/classbasedviews_22_0.txt
## Usage in your URLconf ¶
Django_meta/classbasedviews_21_0.txt
Django provides base view classes which will suit a wide range of applications. All views inherit from the [ ` View ` ](../../ref/class-based- views/base/#django.views.generic.base.View "django.views.generic.base.View") class, which handles linking the view into the URLs, HTTP method dispatching and other common featu...
Django_meta/modelspy_7_0.txt
class Language(models.Model): """Model representing a Language (e.g. English, French, Japanese, etc.)""" name = models.CharField(max_length=200, unique=True, help_text="Enter the book's natural language (e.g. English, French, Japanese etc.)")
Django_meta/tutorialdjango_217_0.txt
2. At the insertion point in the "title" block, write ` Home ` , and in the "content" block, write ` <p>Home page for the Visual Studio Code Django tutorial.</p> ` , then save the file. These lines are the only unique parts of the extended page template:
Django_meta/tutorialdjango_342_0.txt
[ ![Microsoft homepage](/assets/images/microsoft-logo.png) ![Microsoft homepage](/assets/images/microsoft-logo-inverted.png) ](https://www.microsoft.com) © 2024 Microsoft
Django_meta/tutorialdjango_257_0.txt
2. In the ` templates/hello ` folder, create a new template named ` log_message.html ` with the following contents, which assumes that the template is given a variable named ` form ` to define the body of the form. It then adds a submit button with the label "Log".
Django_meta/modelspy_25_0.txt
def __str__(self): """String for representing the Model object.""" return f'{self.id} ({self.book.title})'
Django_meta/classbasedviews_78_0.txt
## Get Involved
Django_meta/introduction_9_0.txt
## Why Django
Django_meta/tutorialdjango_68_0.txt
python manage.py startapp hello
Django_meta/classbasedviews_3_0.txt
Toggle theme (current theme: auto)
Django_meta/tutorialdjango_286_0.txt
from hello.models import LogMessage
Django_meta/django_models_19_0.txt
## Follow Us * [ 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/introduction_30_0.txt
In **Chapters 6-9** , we’re ready for a _Blog_ app that implements CRUD (Create-Read-Update-Delete) functionality. Using Django’s generic class-based views, we only have to write a small amount of actual code for this. Then we’ll add forms and integrate Django’s built-in user authentication system for signup, login, an...
Django_meta/tutorialdjango_159_0.txt
Serving static files in Django is something of an art, especially when deploying to production. What's shown here is a simple approach that works with the Django development server and also a production server like Gunicorn. A full treatment of static files, however, is beyond the scope of this tutorial, so for more in...
Django_meta/tutorialdjango_27_0.txt
2. In that folder, use the following command (as appropriate to your computer) to create a virtual environment named ` .venv ` based on your current interpreter:
Django_meta/django_models_2_0.txt
## Quick example ¶ This example model defines a ` Person ` , which has a ` first_name ` and ` last_name ` : from django.db import models class Person(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) ` first_name ...
Django_meta/tutorialdjango_253_0.txt
With your models in place and the database migrated, you can store and retrieve data using only your models. In this section, you add a form page to the app through which you can log a message. You then modify the home page to display those messages. Because you modify many code files here, be mindful of the details.
Django_meta/tutorialdjango_339_0.txt
* 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 base tem...
Django_meta/tutorialdjango_336_0.txt
* [ Editing Python code ](/docs/python/editing) * [ Linting ](/docs/python/linting) * [ Managing Python environments ](/docs/python/environments) * [ Debugging Python ](/docs/python/debugging) * [ Testing ](/docs/python/testing)
Django_meta/tutorialdjango_317_0.txt
1. Create a superuser account in the app by opening a Terminal in VS Code for your virtual environment, then running the command ` python manage.py createsuperuser --username=<username> --email=<email> ` , replacing ` <username> ` and ` <email> ` , of course, with your personal information. When you run the command, Dj...
Django_meta/tutorialdjango_227_0.txt
With all the page templates in place, save ` views.py ` , run the app, and open a browser to the home page to see the results. Navigate between the pages to verify that the page templates are properly extending the base template.
Django_meta/tutorialdjango_267_0.txt
if request.method == "POST": if form.is_valid(): message = form.save(commit=False) message.log_date = datetime.now() message.save() return redirect("home") else: return render(request, "hello/log_message.html", {"form": form})
Django_meta/introduction_26_0.txt
The book begins by demonstrating how to configure a local development environment for Windows and macOS in **Chapter 1** . We learn about the powerful command line, Git, text editors, and how to install the latest versions of Python and Django.
Django_meta/django_meta_22_0.txt
### ` index_together ` ¶ ` Options. ` ` index_together ` ¶ Use the ` indexes ` option instead. The newer ` indexes ` option provides more functionality than ` index_together ` . ` index_together ` may be deprecated in the future. Sets of field names that, taken together, are indexed: ...
Django_meta/tutorialdjango_151_0.txt
from django.shortcuts import render
Django_meta/tutorialdjango_303_0.txt
5. When you're ready, select Continue ( F5 ) to finish running the app and view the rendered page in the browser. Stop the debugger when you're done.
Django_meta/tutorialdjango_260_0.txt
3. In the app's ` static/hello/site.css ` file, add a rule to make the input form wider:
Django_meta/introduction_33_0.txt
While it may be tempting to skip around in this book, I highly recommend reading the chapters in order. Each chapter introduces a new concept and builds upon past teachings.
Django_meta/tutorialdjango_17_0.txt
The completed code project from this Django tutorial can be found on GitHub: [ python-sample-vscode-django-tutorial ](https://github.com/microsoft/python- sample-vscode-django-tutorial) .
Django_meta/classbasedviews_68_0.txt
Search for information in the archives of the django-users mailing list, or post a question. [ #django IRC channel ](irc://irc.libera.chat/django)
Django_meta/tutorialdjango_164_0.txt
2. In that same file, add the following line at the end, which includes standard static file URLs to the list that the project recognizes:
Django_meta/tutorialdjango_272_0.txt
8. Enter a message, select **Log** , and you should be taken back to the home page. The home page doesn't yet show any of the logged messages yet (which you remedy in a moment). Feel free to log a few more messages as well. If you want, peek in the database using a tool like SQLite Browser to see that records have been...