django_tutorial-blog's People
django_tutorial-blog's Issues
Install Python other version, can't fetch last version django(3.0.2)
After install 3.7 version (PyInstaller currently doesn't support 3.8., so revise it to 3.7, see reference),
In terminal call pip to install django last version but can not fetch it.
(It use py35 pip and py35 pip can not install django version above 3)
Create Web App - Post
- create post class in models.py
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
# one author may have multi post but one post only have one author
# so this is a m2o field
author = models.ForeignKey(User, on_delete=models.CASCADE)
- makemigrations
python manage.py makemigrations
this command create a file on blog\migrations\
called 0001_initial.py.
if you want to check what this migrate file will do.
(app_name/migrations/xxxx_initial.py)
python manage.py sqlmigrate blog 0001
- execute migrate
python manage.py migrate
then we can enter python shell to execute query command
python manage.py shell
>>> from blog.models import Post
>>> from django.contrib.auth.models import User
>>> User.objects.all()
<QuerySet [<User: jhe>]>
>>> u = User.objects
>>> u.filter(username='jhe')
<QuerySet [<User: jhe>]>
>>> u.filter(username='jhe').first()
<User: jhe>
# check Post records
>>> Post.objects.all()
<QuerySet []>
# Create first record on Post
>>> post_1 = Post(title='Test', content='First content from shell', author=u.get(id=1))
>>> Post.objects.all()
<QuerySet []>
# Save it
>>> post_1.save()
>>> Post.objects.all()
<QuerySet [<Post: Post object (1)>]>
Forgot User's password
python manage.py shell
from django.contrib.auth.models import User
# reset password
user = User.objects.get(username='root') user.set_password('newpassword')
user.save()
# list superuser
User.objects.filter(is_superuser=True)
# list all user
User.objects.all()
Register app form.is_valid() = False
After filled name,password and recheck password, form.is_valid() still get false.
Blog template design with RWD
<main>
<div class="nav">固定寬度</div>
<div class="content">動態寬度</div>
<div class="side">固定寬度</div>
</main>
main{
background-color:white;
height: 100%;
width: 100%;
display:inline-block;
position: absolute;
top:0;
left:0;
display:table;
}
.nav{
color:white;
display: table-cell;
height: 100%;
background-color: green;
line-height: 20px;
margin-bottom: 20px;
width: 45px;
z-index: 87;
}
.content{
color:white;
display: table-cell;
background-color:blue;
height: 100%;
}
.side{
color:white;
display: table-cell;
background-color:red;
width: 200px;
height: 100%;
}
@media (max-width: 768px) {
.nav{
background-color: black !important;
position:fixed !important;
height:75px !important;
width:100% !important;
top:0;
left:0;
}
.content{
position:absolute !important;
display: inline-block !important;
height:100% !important;
width:100% !important;
top: 75px !important;
}
.side{
display: none !important;
}
}
backup
<div class="header">
<div class="header-inner">
<nav class="site-nav">
<div class="menu">
<a class="website-text-logo blog-home" href="{% url 'blog_home' %}" title="部落格">
<i class="fa fa-home"></i>
</a>
<a class="website-text-logo" href="{% url 'blog_about' %}" title="關於作者">
<i class="fa fa-user"></i>
</a>
</div>
<div class="cus-menu">
{% if user.is_authenticated %}
<a class="website-text-logo" href="{% url 'profile' %}" title="個人資料">
<i class="fa fa-address-card"></i>
</a>
<a class="website-text-logo" href="{% url 'logout' %}" title="登出">
<i class="fa fa-sign-out"></i>
</a>
{% else %}
<a class="website-text-logo" href="{% url 'register' %}" title="註冊(不開放)">
<i class="fa fa-pencil-square-o"></i>
</a>
<a class="website-text-logo" href="{% url 'login' %}" title="登入">
<i class="fa fa-sign-in"></i>
</a>
{% endif %}
</div>
</nav>
</div>
</div>
<div role="main" class="container">
<div class="row">
<div class="col-md-8">
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% block content %}{% endblock %}
</div>
<!-- <div class="col-md-4">-->
<!-- <div class="content-section">-->
<!-- <h3>Our Sidebar</h3>-->
<!-- <p class='text-muted'>You can put any information here you'd like.-->
<!-- <ul class="list-group">-->
<!-- <li class="list-group-item list-group-item-light">Latest Posts</li>-->
<!-- <li class="list-group-item list-group-item-light">Announcements</li>-->
<!-- <li class="list-group-item list-group-item-light">Calendars</li>-->
<!-- <li class="list-group-item list-group-item-light">etc</li>-->
<!-- </ul>-->
<!-- </p>-->
<!-- </div>-->
<!-- </div>-->
</div>
</div>
Using PostgreSQL with Django
- Download PostgreSQL
remember default password for postgres - create postgreSQL user and set privileges
- create database name and set parameters
ALTER ROLE myuser SET client_encoding TO 'utf8';
ALTER ROLE myuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myuser SET timezone TO 'UTC';
- terminal
pip install psycopg2
and modified settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'djtutorial',
'USER': 'djadmin',
'PASSWORD': 'djadmin',
'HOST': 'localhost',
'PORT': '',
}
}
How to get current user in Post model?
I would like to get current user in Post model which is class to deal with blog posts.
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
# one author may have multi post but one post only have one author
# so this is a m2o field
# to get self function, we should define function before use it.
author = models.ForeignKey(User, on_delete=models.CASCADE)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.