Skip to content

Commit 5623350

Browse files
committed
Added example .env file.
1 parent 8d811cb commit 5623350

File tree

16 files changed

+59
-23
lines changed

16 files changed

+59
-23
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,20 @@ $ python3 manage.py runserver
4040

4141
## Configuration
4242

43-
Configuration is handled by creating a **django_views_tutorial/.env** file. This should contain the following variables (replace the values with your own):
43+
Configuration is handled by creating a **django_views_tutorial/.env** file (see **.env.example** for reference) Replace values with your own:
4444

4545
```.env
46-
SECRET_KEY="YOURSECRETKEY"
46+
DEBUG=True
47+
SECRET_KEY="yoursecretkey"
4748
DJANGO_SETTINGS_MODULE="django_views_tutorial.settings"
48-
DATABASE_ENGINE="django.db.backends.mysql"
49-
DATABASE_NAME="mydatabasename"
50-
DATABASE_USER="user"
51-
DATABASE_PASSWORD="password"
52-
DATABASE_HOST="mydatabase.mycloud.com"
53-
DATABASE_PORT=3306
54-
DATABASE_CERTIFICATE="../creds/ca-certificate.crt" (optional)
49+
50+
DATABASE_ENGINE=django.db.backends.mysql
51+
DATABASE_NAME=databasename
52+
DATABASE_USER=username
53+
DATABASE_PASSWORD=password
54+
DATABASE_HOST=0.0.0.0
55+
DATABASE_PORT=1234
56+
DATABASE_CERTIFICATE="../creds/ca-certificate.crt" # (optional)
5557
```
5658

5759
-----

class_views/forms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
"""Form classes."""
12
from django import forms
23

34

45
class ContactForm(forms.Form):
6+
"""Email submission form."""
57
subject = forms.CharField(required=True, label="Subject line", strip=True)
68
email = forms.EmailField(required=True, label="Your email")
79
message = forms.CharField(widget=forms.Textarea)

class_views/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Data models."""
12
from django.db import models
23

34

class_views/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Class-based view example URLs."""
12
from django.conf import settings
23
from django.urls import path
34
from django.conf.urls.static import static

class_views/views.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
"""Examples of class-based views."""
12
from datetime import datetime
23

34
from django.http import HttpResponseRedirect
45
from django.conf import settings
5-
66
from django.views import View
77
from django.views.generic import RedirectView
88
from django.views.generic.base import TemplateView
99
from django.views.generic.edit import FormView
10-
1110
from django.shortcuts import render, get_object_or_404
1211
from django.contrib.auth.mixins import PermissionRequiredMixin
1312
from django.core.mail import send_mail
@@ -17,6 +16,7 @@
1716

1817

1918
class GenericClassView(View):
19+
"""Generic class-based view."""
2020
initial = {'key': 'value'}
2121

2222
def get(self, request, *args, **kwargs):
@@ -32,6 +32,7 @@ def post(self, request, *args, **kwargs):
3232

3333

3434
class TemplateClassView(TemplateView):
35+
"""Serve a page template."""
3536
template_name = 'class_views/template_class_view.html'
3637

3738
# change the get method to get_context_data
@@ -44,13 +45,16 @@ def get_context_data(self, **kwargs):
4445

4546

4647
class ProtectedClassView(PermissionRequiredMixin, TemplateView):
48+
"""Serve a page template to authorized users."""
49+
4750
def get_context_data(self, **kwargs):
4851
context = super().get_context_data(**kwargs)
4952
context['now'] = datetime.now()
5053
return context
5154

5255

5356
class RedirectClassView(RedirectView):
57+
"""Permanent redirect view."""
5458
# https://docs.djangoproject.com/en/3.0/ref/class-based-views/base/#redirectview
5559
permanent = False
5660
query_string = True
@@ -63,6 +67,7 @@ def get_redirect_url(self, *args, **kwargs):
6367

6468

6569
class ContactView(FormView):
70+
"""Form-based view."""
6671
template_name = 'class_views/contact_class_template.html'
6772
form_class = ContactForm
6873
success_url = '/success/'

django_views_tutorial/.env.example

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
DEBUG=True
2+
SECRET_KEY="yoursecretkey"
3+
DJANGO_SETTINGS_MODULE="django_views_tutorial.settings"
4+
5+
DATABASE_ENGINE=django.db.backends.mysql
6+
DATABASE_NAME=databasename
7+
DATABASE_USER=username
8+
DATABASE_PASSWORD=password
9+
DATABASE_HOST=0.0.0.0
10+
DATABASE_PORT=1234
11+
DATABASE_CERTIFICATE="../creds/ca-certificate.crt"

django_views_tutorial/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
"""Django's settings."""
12
import os
23
import environ
34
import mimetypes
45

56
mimetypes.add_type("text/css", ".css", True)
67

7-
root = environ.Path(__file__) - 3 # get root of the project
88
env = environ.Env()
9-
environ.Env.read_env() # reading .env file
9+
environ.Env.read_env()
1010

1111
# False if not in os.environ
1212
DEBUG = env('DEBUG')

function_views/forms.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
"""Form classes."""
12
from django import forms
23
from django.forms import CharField, Textarea
34

45

56
class GuestBookForm(forms.Form):
6-
name = CharField(label="Your name",
7-
required=True,
8-
strip=True)
9-
msg = CharField(label="Leave a message.",
10-
required=True,
11-
widget=Textarea(attrs={'cols': '30', 'rows': '5'}),
12-
min_length=10)
7+
"""Form to accept guestbook submissions."""
8+
name = CharField(
9+
label="Your name",
10+
required=True,
11+
strip=True
12+
)
13+
msg = CharField(
14+
label="Leave a message.",
15+
required=True,
16+
widget=Textarea(attrs={'cols': '30', 'rows': '5'}),
17+
min_length=10
18+
)
1319

function_views/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
"""Data models."""
12
from django.db import models
23

34

45
class User(models.Model):
5-
"""User account."""
6+
"""User account model."""
67
id = models.AutoField(primary_key=True)
78
name = models.CharField(max_length=50)
89
email = models.EmailField(null=False)
@@ -17,7 +18,7 @@ class User(models.Model):
1718

1819

1920
class Message(models.Model):
20-
"""Message left by a user in a guest book."""
21+
"""Guestbook message model."""
2122
id = models.AutoField(primary_key=True)
2223
name = models.CharField(max_length=255)
2324
message = models.TextField()

function_views/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
"""Function-based view example URLs."""
12
from django.conf import settings
23
from django.urls import path
34
from django.conf.urls.static import static
4-
55
from . import views
66

77
urlpatterns = [

0 commit comments

Comments
 (0)