Best VPN Service (English Banners)

GeekBuying.com

Using Django to Build a Website (Mac) 3

Alibaba Cloud
Autonomous Standing Desk

System Mechanic® Ultimate Defense™
Save up to 50% on ANSI Standards Packages

(project1_env) SS-MacBook:src liveuk$ ls

db.sqlite3 manage.py products trydjango

IK Multimedia's T-RackS 5

(project1_env) SS-MacBook:src liveuk$ python manage.py shell

Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37)

[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin

Autonomous Standing Desk

Type “help”, “copyright”, “credits” or “license” for more information.

(InteractiveConsole)

>>> from products.models import Product

>>> #import class Product

>>> Product.object.all()

Traceback (most recent call last):

File “<console>”, line 1, in <module>

AttributeError: type object ‘Product’ has no attribute ‘object’

>>> Product.objects.all()

<QuerySet [<Product: Product object (1)>, <Product: Product object (2)>]>

>>> Product.objects.create(title=’new pro3′, description = ‘another one’, price=’123′)

<Product: Product object (3)>

>>>

KeyboardInterrupt

>>>

>>>

[1]+  Stopped                 python manage.py shell

 

from django.db import models

# Create your models here.
class Product(models.Model):
title = models.CharField(max_length=120) #max_length = required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=1000)
summary = models.TextField(blank=False, null=False)#default=’Must Buy’)
featured = models.BooleanField() #null=True, default=True

OR TRY: (AFTER, DO python manage.py makemigrations and migrate

summary = models.TextField(blank=True, null=False)#default=’Must Buy’)

Start over again (come back later, deleted wrong files)

  • delete all the files in the migrations folder: admin.py apps.py models.py tests.py and views.py. keep __init__.py
  • delete __pycache__
  • delete db.sqlite3

 

python manage.py startapp pages add pages into settings

views.py (1:01:05) change from:

from django.shortcuts import render

# Create your views here.
def home_view():
return “<h1>Hello World</h1>”

to:

from django.http import HttpResponse #define HttpResponse
from django.shortcuts import render

# Create your views here.
def home_view(request,*args, **kwargs): #*args, **kwargs
print(args, kwargs)
print(request.user)
return HttpResponse(“<h1>Hello World</h1>”) #string of HTML code

def contact_view(*args, **kwargs):
return HttpResponse(“<h1>Contact Us</h1>”)

def about_view(*args, **kwargs):
return HttpResponse(“<h1>About Us</h1>”)

def social_view(*args, **kwargs):
return HttpResponse(“<h1>Social Channels</h1>”)

urls.py copy:

path(”, views.home, name=’home’)

to the urlpatters and change views.home to whatever defined in views.py, and import views:

from django.contrib import admin
from django.urls import path

#from pages import views
from pages.views import home_view
from pages.views import contact_view
from pages.views import about_view
from pages.views import social_view

urlpatterns = [
#path(”, views.home_view, name=’home’),
path(”, home_view, name=’home’),
path(‘contact/’, contact_view),
path(‘about/’, about_view),
path(‘social/’, social_view),
path(‘admin/’, admin.site.urls),
]

after adding below to views.py

def home_view(request,*args, **kwargs): #*args, **kwargs
print(args, kwargs)
print(request.user)

you will see:

November 17, 2019 – 16:01:27

Django version 2.0.7, using settings ‘trydjango.settings’

Starting development server at http://127.0.0.1:8000/

Quit the server with CONTROL-C.

() {}

mytestsuperuser

Django Templates

(utilising from django.shortcuts import render which is already there in veiws.py)

change def home_view to:

def home_view(request,*args, **kwargs): #*args, **kwargs
print(args, kwargs)
print(request.user)
#return HttpResponse(“<h1>Hello World</h1>”) #string of HTML code
return render(request, “home.html”,{})

create scr/templates folder

create scr/templates/home.html

<h1>Hello Word</h1>
<p>This is a template</p>

change DIRS in settings.py, copy from BASE_DIR:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

to make DIRS in TEMPLATES:

TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
#’DIRS’: [‘/Users/liveuk/env1/project1_env/src/templates/’],
‘DIRS’: [os.path.join(BASE_DIR,”templates”)],
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]

render the new html views in views.py:

def contact_view(request, *args, **kwargs):
#return HttpResponse(“<h1>Contact Us</h1>”)
return render(request, “contact.html”,{})

def about_view(request, *args, **kwargs):
#return HttpResponse(“<h1>About Us</h1>”)
return render(request, “about.html”,{})

def social_view(request, *args, **kwargs):
return HttpResponse(“<h1>Social Channels</h1>”)

create base.html and navbar.html in templates folder:

<!doctype html>
<html>
<head>
<title>Hello Home is doing Try Django</title>
</head>
<body>
<!– <h1>Nav Bar</h1> –>
{% include ‘navbar.html’ %}

{% block content %}
1 Today’s special
{% endblock %}

{% block content_two %}
2 Speak to one of the reps
{% endblock %}
</body>
</html>

 

<nav>
<ul>
<li>Payment Methods</li>
<li>Contact</li>
<li>About</li>
</ul>
</nav>

{% extends “base.html” %}
{% block content_two %}
<h1>About</h1>
<p>This is a template</p>
{% endblock %}

{% extends “base.html” %}
{% block content %}
<h1>Contact</h1>
<p>This is a template</p>
{% endblock %}

Rendering Context in a Template

in views.py we add the my_context part:

def about_view(request, *args, **kwargs):
#return HttpResponse(“<h1>About Us</h1>”)
my_context = {
“my_text”: “This is about accpeting payments from all your Chinese customers, so far we can help you with upto five payments, reviews number is:”,
“my_number”:100020023,
“my_list”:[‘POS’,’ePOS’,’SmartTerminal’]
}
return render(request, “about.html”, my_context)

in about.html

{% extends “base.html” %}
{% block content %}
<h1>About</h1>
<p>This is a template for About TigerPay</p>
<p>
{{ my_text }} , {{ my_number }}
</p>
<p>
{{ my_list }}
</p>
{% endblock %}

FOR LOOP IN A TEMPLATE

in about.html

{% extends “base.html” %}
{% block content %}
<h1>About</h1>
<p>This is a template for About TigerPay</p>
<p>
{{ my_text }} , {{ my_number }}
</p>
<p>
{{ my_list }}
</p>
<ul>
<li>Easy to install</li>
<li>Training available but you would barely need that</li>
</ul>

<ul>
{% for abc in my_list %}
{% if abc == 312 %}
<li>{{ forloop.counter }} – {{ abc | add:22}}</li>
{% elif abc == “POS” %}
<li>This is the basic and free solution – for EVERYONE</li>
{% else %}
<li>{{ forloop.counter }} – {{ abc }}</li>
{% endif %}

{% endfor %}
</ul>

{% endblock %}

in views.py

def about_view(request, *args, **kwargs):
#return HttpResponse(“<h1>About Us</h1>”)
my_context = {
“my_text”: “This is about accpeting payments from all your Chinese customers, so far we can help you with upto five payments, reviews number is:”,
“my_number”:100020023,
“this_is_true”: True,
“my_list”:[‘POS’,312, 567, ‘SmartTerminal’]
}
return render(request, “about.html”, my_context)

Template Tags and Filters

{% extends “base.html” %}
{% block content %}
<h1>About</h1>
<h3>{{ title |capfirst |upper}}</h3>
<p>About TigerPay – this is a Template</p>
{{ my_html | safe |title}}
{{ my_html | striptags | slugify }}

<p>
{{ title | capfirst }}
, so far we can help you with upto five payments, reviews number is: {{ my_number }}
</p>
<p>
{{ my_list }}
</p>

nordvpn