Skip to main content

DJANGO Startup

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


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

Popular posts from this blog

laravel file manager sorting by time default

Ref :  https://github.com/UniSharp/laravel-filemanager/issues/337 To load files order by "time DESC" you can change the code in vendor/unisharp/laravel-filemanager/src/traits/LfmHelpers.php public function sortFilesAndDirectories($arr_items, $sort_type) { if ($sort_type == 'time') { $key_to_sort = 'updated'; } elseif ($sort_type == 'alphabetic') { $key_to_sort = 'name'; } else { $key_to_sort = 'updated'; } return strcmp($a->{$key_to_sort}, $b->{$key_to_sort}); }); return $arr_items; } with public function sortFilesAndDirectories($arr_items, $sort_type) { if ($sort_type == 'time') { $key_to_sort = 'updated'; } elseif ($sort_type == 'alphabetic') { $key_to_sort = 'name'; } else { $key_to_sort = 'updated'; ...

some important points for web developers and hosting

check dns https://www.whatsmydns.net/ https://check-host.net/ https://www.site24x7.com/ping-test.html attacks https://www.computerhope.com/unix/uping.htm https://vitux.com/linux-ping-command/ https://www.howtoforge.com/linux-ping-command/ https://www.poftut.com/linux-ping-command-tutorial-examples/ https://phoenixnap.com/kb/linux-ping-command-examples https://sandilands.info/sgordon/ping-flooding-dos-attack-in-a-virtual-network https://www.geeksforgeeks.org/ping-command-in-linux-with-examples/

Installing Admin LTE in Laravel

step 1:  Reference---  https://hdtuto.com/article/laravel-56-adminlte-bootstrap-theme-installation-example Step 2 : after completion first reference view this link:    https://github.com/JeroenNoten/Laravel-AdminLTE step 3:  For more additional information view this link:   https://github.com/jeroennoten/Laravel-AdminLTE#2-updating Now you are done statically : https://adminlte.io/blog/integrate-adminlte-with-laravel Steps: 1:  composer require jeroennoten/laravel-adminlte 2: 'providers' => [ .... JeroenNoten\LaravelAdminLte\ServiceProvider::class, ], 3: php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\ServiceProvider" --tag=assets 5: php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\ServiceProvider" --tag=config 6: php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\ServiceProvider" --tag=views 7: for admin pannel php artisan ...