Git Product home page Git Product logo

django_tutorial-blog's People

Contributors

arjhe avatar

Watchers

 avatar  avatar

django_tutorial-blog's Issues

Create Web App - Post

  1. 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)
  1. makemigrations
python manage.py makemigrations

20012502
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

20012503

  1. execute migrate
python manage.py migrate

20012504

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()

Blog template design with RWD

template design in codepen

<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

  1. Download PostgreSQL
    remember default password for postgres
  2. create postgreSQL user and set privileges
    20012501
  3. 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';
  1. 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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.