https://www.ostechnix.com/install-django-web-framework-ubuntu-16-04/
https://stackoverflow.com/questions/46210934/importerror-couldnt-import-django
create a folder
now in cmd:
go to that folder
make virtual environment there:
virtualenv py1
now activate the environment:
.\py1\Scripts\activate
then virtual env is activated:
now install django
python -m pip install django
: to install django in project
create project:
django-admin startproject projectname
now open project in vscode and
pip install mysqlclient
: to install mysql in django
for going to py1 environment from cmd
workon py1
inside settings.py write this code to connect with mysql , for that first create a database in mysql then connect it to django via settings.py
now,
python manage.py runserver
: to run the server i.e. localhost:8000 in the browser
now to create super user from command in django
python manage.py createsuperuser -- username=admin --email=admin@admin.com
run this command then it ask you for password and follow instruction in command line.
login with username and password
to create an app : python manage.py startapp posts
then include posts inside settings.py -> installedapps function
'posts',
then inside urls.py include url of urls.py i.e. to be created inside posts app
#note:
path is imported from django.urls url and include from django.conf.urls
admin from django.contrib
urlspy of posts app
views .py of posts app
here "posts/index.html" means inside of post app create template folder inside
which create "post" folder and inside that create "index.html"
as master blade in laravel create a layout for head and footer tag
create layout.html inside post floder of posts app
we use zinja templeting like blade in laravel
inside index.html
now lets use models.py of posts app
this "verbose_name_plural="Posts" (this is to set the name inside admin panel)
then make migration from command line:
python manage.py makemigrations posts
and
this line will help to show title of the post instead of oblect(1).. in admin pannel
{try with removing this lone and then by adding to find difference}
now it will create migration folder inside posts app and also create initial.py which is
similar as create_xyz_tables inside of migrations
now to migrate i.e. to create table in database :
python manage.py migrate
now to register "posts" model in admin pannel i.e. to show in admin pannel and to
do crude :
inside admin.py
from .models import posts
(here posts is the class that we made inside models.py)
#understanding
models.py maa class define garyaa hunxa ,
ani schema define hunxa class vitra
eg: title body date etc
views.py maa as controller in laravel fn like index, detail haru define hunxa
urls.py maa url haru define as route
settings.py maa application ko name register hunnxa
admin.py maa models ko class haru lai import and register garinxa for admin view
tampletes vanni folder vitra html file haru rakhinxa for viewing in browser
https://stackoverflow.com/questions/46210934/importerror-couldnt-import-django
create a folder
now in cmd:
go to that folder
make virtual environment there:
virtualenv py1
now activate the environment:
.\py1\Scripts\activate
then virtual env is activated:
now install django
python -m pip install django
: to install django in project
create project:
django-admin startproject projectname
now open project in vscode and
pip install mysqlclient
: to install mysql in django
for going to py1 environment from cmd
workon py1
DATABASES={
'default':{
'ENGINE':'django.db.backends.mysql',
'NAME':'djangoproject',
'USER':'root',
'PASSWORD':'',
'HOST':'localhost',
'PORT':''
}
}
inside settings.py write this code to connect with mysql , for that first create a database in mysql then connect it to django via settings.py
now,
python manage.py runserver
: to run the server i.e. localhost:8000 in the browser
now to create super user from command in django
python manage.py createsuperuser -- username=admin --email=admin@admin.com
run this command then it ask you for password and follow instruction in command line.
login with username and password
to create an app : python manage.py startapp posts
then include posts inside settings.py -> installedapps function
'posts',
then inside urls.py include url of urls.py i.e. to be created inside posts app
#note:
from django.conf.urls import url,include
from django.contrib import admin
from django.urls import path
from . import views
path is imported from django.urls url and include from django.conf.urls
admin from django.contrib
urlpatterns = [
path('admin/', admin.site.urls),
path('posts/',include('posts.urls')),
# url(r'^posts/',include('posts.urls')),
# url(r'^admin/',admin.site.urls)
]
urlspy of posts app
from django.conf.urls import url
from django.urls import path
from . import views
urlpatterns=[
# url(r'^$',views.index,name='index')
path('',views.index,name='index')
];
views .py of posts app
from django.shortcuts import render
from django.http import HttpResponse
from .models import posts
# Create your views here.
def index(request):
# return HttpResponse('HEllo from post')
# posts=posts.objsct.all()
poststmp = posts.objects.all()[:10]
# for showing 10
context={
'title':'Latest Post',
'postsinview':poststmp
}
return render(request,'post/index.html',context)
here "posts/index.html" means inside of post app create template folder inside
which create "post" folder and inside that create "index.html"
as master blade in laravel create a layout for head and footer tag
create layout.html inside post floder of posts app
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" />
<title>First App</title>
</head>
<body>
<header class="container center-align">
<h1>Sabin bajgain Blog</h1>
</header>
<div class="container">
{%block content %}
{%endblock%}
<br>
<a href="/admin">Admin login</a>
</div>
</body>
</html>
we use zinja templeting like blade in laravel
inside index.html
{%extends 'post/layout.html'%}
{%block content%}
<h1 class="center-align red lighten-3">{{title}}</h1>
<ul>
{%for post in postsinview%}
<li><a href="/posts/details/{{post.id}}">{{post.title}}</a></li>
{%endfor%}
</ul>
{%endblock%}
now lets use models.py of posts app
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()
ctreated_at=models.DateTimeField(default=datetime.now,blank=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural="Posts"
this "verbose_name_plural="Posts" (this is to set the name inside admin panel)
then make migration from command line:
python manage.py makemigrations posts
and
def __str__(self):
return self.title
this line will help to show title of the post instead of oblect(1).. in admin pannel
{try with removing this lone and then by adding to find difference}
now it will create migration folder inside posts app and also create initial.py which is
similar as create_xyz_tables inside of migrations
now to migrate i.e. to create table in database :
python manage.py migrate
now to register "posts" model in admin pannel i.e. to show in admin pannel and to
do crude :
inside admin.py
from .models import posts
(here posts is the class that we made inside models.py)
from django.contrib import admin
from .models import posts
admin.site.register(posts)
# Register your models here.
#understanding
models.py maa class define garyaa hunxa ,
ani schema define hunxa class vitra
eg: title body date etc
views.py maa as controller in laravel fn like index, detail haru define hunxa
urls.py maa url haru define as route
settings.py maa application ko name register hunnxa
admin.py maa models ko class haru lai import and register garinxa for admin view
tampletes vanni folder vitra html file haru rakhinxa for viewing in browser
Comments
Post a Comment