It’s time to show off your skills by bringing “Vanilla” Django and Django Rest Framework together in the same project.
You’ll build out a Restful API as well as a user-facing site.
Along the way you’ll see how easy Django makes it to move to a remote database.
The project will be an old favorite with a Python twist - a Cookie Stand management site.
requirements.txt.things app folder to be cookie_stands.Thing, thing, and things change to cookie-stand related names.
Thing model becomes CookieStandThingList becomes CookieStandListthing. Search should be case insensitive..env using .env.sample as a starting point.
python -c “import secrets; print(secrets.token_urlsafe())”
The CookieStand model must contain:
location = models.CharField(max_length=256)owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, null=True, blank=True)description = models.TextField(blank=True)hourly_sales = models.JSONField(default=list, null=True)minimum_customers_per_hour = models.IntegerField(default=0)maximum_customers_per_hour = models.IntegerField(default=0)average_cookies_per_sale = models.FloatField(default=0)def __str__(self):
return self.location
JSONField.hourly_sales is not provided at creation time it will be generated with random numbers based on minimum/maximum customers per hour and average cookies per sale.cookie-stand-api.docker compose up --builddocker compose downdocker compose restartdocker compose run web python manage.py migratedocker compose run web python manage.py collectstaticcookie_stands/tests.py.createsuperuser has been run..env contents or provide in Canvas submission.Bookmark and Review
Statement on why this topic matter as it relates to what I’m studying in this module:
Understanding Django settings, WhiteNoise, and CORS in is crucial for building secure, scalable, and efficient web apps.
What are the key principles to follow when organizing and configuring Django settings for a project, according to the “Django Settings Best Practices” reading?
Keep settings in environment variables: Storing settings in environment variables allows for better separation of configuration from code and enhances security by keeping sensitive information out of version control systems.
Write default values for production configuration: It’s advisable to provide default values for production configuration settings, excluding sensitive ones like secret keys and tokens.
Don’t hardcode sensitive settings: Sensitive settings such as secret keys and tokens should not be hardcoded in the settings files, and they should not be included in version control systems.
Split settings into groups: Organize settings into groups such as Django settings, third-party settings, and custom project settings to maintain clarity and manageability.
Follow naming conventions: Use meaningful names for custom project settings, prefixing them with the project name, and provide descriptions for clarity and documentation purposes.
How does the White Noise library contribute to the efficient serving of static files in a Django application, and what are the steps to integrate it into a project?
by allowing the application to serve its own static files without relying on external services like nginx or Amazon S3. It contributes to the efficient serving of static files by:
Handling compression: WhiteNoise serves compressed content (gzip and Brotli formats) and correctly handles Accept-Encoding and Vary headers, ensuring efficient data transfer between the server and the client.
Setting cache headers: WhiteNoise sets far-future cache headers on static content that is unlikely to change, optimizing caching behavior and improving performance by reducing unnecessary requests to the server.
Steps to integrate WhiteNoise into a project:
Install WhiteNoise using pip:
pip install whitenoise
Add WhiteNoise middleware to the Django project’s middleware list in the settings file:
MIDDLEWARE = [
# Other middleware
"whitenoise.middleware.WhiteNoiseMiddleware",
]
Optionally, configure the project to use compressed and forever-cacheable static files by setting the STATICFILES_STORAGE in the settings file:
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
What is the purpose of Cross-Origin Resource Sharing (CORS) in web applications, and how can it be implemented and configured in a Django project to control access to resources?
can be implemented and configured using middleware or third-party packages. The Django CORS headers package is a popular choice for handling CORS in Django projects. It allows developers to specify which origins are allowed to access resources, configure allowed methods and headers, and control other aspects of CORS behavior.
To implement CORS in a Django project using the Django CORS headers package, developers can follow these steps:
- Install the Django CORS headers package:
```
pip install django-cors-headers
```
- Add `'corsheaders'` to the `INSTALLED_APPS` list in the settings file.
- Add the CORS middleware to the middleware list in the settings file:
```python
MIDDLEWARE = [
# Other middleware
'corsheaders.middleware.CorsMiddleware',
]
```
- Configure CORS settings in the settings file to specify allowed origins, methods, headers, and other options:
```python
CORS_ALLOWED_ORIGINS = [
"http://example.com",
"https://example.com",
]
```
Retrospectives are a critical part of Agile, and typically take the form of meetings held by a team at the end of a sprint cycle. To get us acclimated to that process, we will use the format of a retrospectives to guide today’s reflection.
This article gives a nice overview to the role of retrospectives.