-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDjango.txt
199 lines (132 loc) · 4.59 KB
/
Django.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
###Initial :
1. open terminal
2. go to projects directory
2. mkvirtualenv py1 (for creating virtual environment)
later access it by -> workon py1
2. To install virtualenv in that project directory
- python -m venv ./venv
- ./venv/Scripts/activate.bat or follow below line
- C:\Users\tamim\PyProjects\E_ApartmentShop\venv\Scripts\activate.bat (Full path of that file)
3. pip install django==2.1
4. django-admin help (for various command help )
###Create Project :
1. django-admin startproject proj_name . (you must put . to create on that directory)
2. go to project directory
3. code . (to start with vscode)
***4. python manage.py runserver
5. copy following mysql file to project folder
6. pip install mysqlclient-1.4.2-cp37-cp37m-win32.whl
###Database Connect :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database_name',
'USER' : 'root',
'Password' : '',
'HOST' : 'localhost',
'PORT' : ''
}
}
->python manage.py makemigrations
->python manage.py migrate
<!------------------------- New Learning in this project ------------------------------------------->
### Add new column or Database table change
-> delete migration file except __init__.py
-> python manage.py makemigrations posts [posts is app name]
-> python manage.py migrate --fake posts zero
-> python manage.py migrate posts
otherwise --
Initialize migrations for your existing models:
./manage.py makemigrations myapp
Fake migrations for existing models:
./manage.py migrate --fake myapp
Add the new field to myapp.models:
from django.db import models
class MyModel(models.Model):
... #existing fields
newfield = models.CharField(max_length=100) #new field
Run makemigrations again (this will add a new migration file in migrations folder that add the newfield to db):
./manage.py makemigrations myapp
Run migrate again:
./manage.py migrate myapp
### adding login required function such as creating post :
-> See this :
- Posts:view
- login.html
- login:view
<!------------------------- Ending ------------------------------------------------------------------>
###Create superuser
-> python manage.py createsuperuser --username=tamim [email protected]
-> give password
###Starting
-> python manage.py startapp posts
-> go to django->settings.py
-> INSTALLED_APPS =[copy here
'posts',
->goto urls djangoproject and copy
from django.contrib import admin
from django.conf.urls import url,include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/', include('posts.urls'))
]
-> goto urls posts
from django.conf.urls import url
from . import views
urlpatterns = [
url('^$', views.index, name = 'index')
];
-> goto views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
#return HttpResponse('hello from posts')
return render(request, 'posts/index.html')
-> make folder templates/posts
-> create index.html // using jinja template from layout.html
{% extends 'posts/layout.html' %}
{% block content %}
<h1>hllllllllllllllllllll</h1>
{% endblock %}
-> create layout.html
<div class="container">
{% block content %}
{% endblock %}
</div>
-> create model for database posts/models.py
from django.db import models
from datetime import datetime
# Create your models here.
class Posts(models.Model):
title = models.CharField(max_length = 200)
body = models.TextField()
created_at = models.DateTimeField(default=datetime.now, blank=True)
-> at terminal
1. python manage.py makemigrations posts
2. python manage.py migrate
-> add to admin.py
from .models import Posts
admin.site.register(Posts)
-> add to models.py
//to show name of the posts
def __str__(self):
return self.title
//to show Posts instead of postss
class Meta:
verbose_name_plural = "Posts"
-> go to views.py
from .models import Posts
posts = Posts.objects.all()[:10]
///Latest Learned
->To load static folder (css, js, html, img)
- python manage.py collectstatic
->Github instruction
- git init
- add .gitignore in that directory (by searching gitigonre.io and search django copy and paste)
- git add --all
- git commit -m "initial commit"
- git remote add origin <Link to GitHub Repo> //maps the remote repo link to local git repo
- git remote -v //this is to verify the link to the remote repo
- git push -u origin master