---
# System prepended metadata

title: django expense project
tags: [django, python, project]

---

# django expense project
###### tags: `python` `django` `project`

### project link
https://djangoexpenseprojectapp.herokuapp.com/authentication/login?next=/

### other note
https://gitmind.com/app/doc/84846340b87aad15425f28fcf8a24a16

# source
video:
https://www.youtube.com/watch?v=a2Rom1nfHRs&list=PLx-q4INfd95G-wrEjKDAcTB1K-8n1sIiz&index=23&ab_channel=CryceTrulyCryceTruly

source code:
https://github.com/CryceTruly/trulyexpensesyoutube

### prework
source venv/bin/activate

CREATE DATABASE djangoExpenseProject;
GRANT CONNECT ON DATABASE djangoExpenseProject TO test;



work flow:
create app
create project1
create views of project1


### deploy
heroku
https://devcenter.heroku.com/articles/django-app-configuration
```
//Procfile
pip install gunicorn

//settings.py changes
pip install django-heroku

```

libpq5 php
與
libpq-dev python
衝突

heroku set up
make Procfile

run on heroku server:
```
gunicorn djangoExpenseProject.wsgi
```

```
//install heroku cli
sudo snap install --classic heroku

//login heroku cli
heroku login

//create app
heroku create djangoexpenseprojectapp

 ›   Warning: heroku update available from 7.52.0 to 7.54.0.
Creating ⬢ djangoexpenseprojectapp... done
https://djangoexpenseprojectapp.herokuapp.com/ | https://git.heroku.com/djangoexpenseprojectapp.git

//push to heroku
git push heroku 

//run command on heroku vm
heroku run bash

//migrate on heroku
python manage.py migrate



```

## Register

### register varify

for the restapi function add the rest framework
```
pip install djangorestframework
pip install markdown       # Markdown support for the browsable API.
pip install django-filter  # Filtering support
```

use ajax to show username and email validation

vahttps://hackmd.io/lidate email(same as validate username)
```
pip install validate-email
//the library is a little bit old(2015)
//maybe can find a newer one  
```

make usernameSuccessOutput to give user the checking
when the net speed is running low

email varify

### register send email
add setting.py
```
#email
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
DEFAULT_FROM_EMAIL = os.environ.get('EMAIL_HOST_USER')
EMAIL_USE_TLS = True
EMAIL_PORT = 587 #gmail
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')

```

change gmail security setting

https://myaccount.google.com/lesssecureapps?pli=1&rapt=AEjHL4NnRNcyXBMwX6a1XQGCH1LGwhohNI-TqrxdWfOJ50e8fVQie_iILKEzAWSbMed8YY1GzkVHQmw7aluKbfMaYaraBuvLbQ

### use email send token to verify
add url
make token
```
//
pip install six
```

#### check data
```
python manage.py shell
//check all user
>>> from django.contrib.auth.models import User
>>> User.objects.all() 

<QuerySet [<User: test>, <User: lj>]>

//check user
>>> user = User.objects.get(username='lj') 

//delete all user
>>> User.objects.all().delete() 
(2, {'auth.User': 2})

```
 
### login & logout
protext route (can't go back from logout to login directly)

### design the add_expenses page

breadcrumb:
https://getbootstrap.com/docs/4.0/components/breadcrumb/


### the expense model

create superuser
```
(venv) lj@lj-Aspire-V3-471G:~/PycharmProjects/djangoExpenseProject$ python manage.py createsuperuser
Username: admin
Email address: admin@gmail.com
Password: admin
Password (again):admin 

```

### expense CRUD

### use ajax to search

### user preference
settings
user prefernece
model
migration
project url
urls
view
add to index view

### income
startapp
model
setting
make migrations
migrate

### Question
libpq5 php
與
libpq-dev python
衝突


Q: the checking when the net speed is running low is very annoying

.env 如何使用

str byte

### all function in income

### customize django admin
make /home/lj/PycharmProjects/djangoExpenseProject/templates/admin/base_site.html
```
{% extends 'admin/base.html' %}
{% load static %}

{% block branding %}
    <h1 id="head">
      <img src="{% static 'img/logo.png' %}" width="35" height="35" />
      Django Expense Project
    </h1>
{% endblock %}
{% block extrastyle%}

<link rel="stylesheet" href="{% static 'css/adminstyle.css'%}" />

{%endblock %}


```

### reset password
use PasswordResetTokenGenerator to generate token
other same as activate user

use multi threading to speed up the send email time

### chart.js


login:
Error:
NoReverseMatch at /authentication/login
Reverse for '' not found. '' is not a valid view function or pattern name.
https://stackoverflow.com/questions/45724006/django-reverse-for-not-found-is-not-a-valid-view-function-or-pattern-na
https://www.chartjs.org/docs/latest/general/colors.html

### export csv, xlsx, pdf

csv: build in
xlsx
```
  pip install xlwt

```
pdf
```
pip install weasyprint
```





### need to deep dive
the way django generate token

structure of django auth, because now all I used is build in 
I can't really understand how it works
django pagination


### 之後對專案的想法
use mongo db
serialize the category db
show default date when add, edit(finished)
缺點：為了用ajax實現查詢,程式碼參雜python和js->改成全部使用python
在建立一個index 不要把expense當首頁
statistic 獨立件一個 app




### 使用說明
http://127.0.0.1:8000/admin
登入


### 專案中學習到的

### 使用技術

#### django
user system
admin panel

#### frontend
visualize data(chart.js)
bredcrump(html)
django render template

#### deploy
heroku deploy

export file(csv, excel, pdf)