In the last tutorial we designed a project. Project holds the whole infrastructure, it can contain multiple apps. A weblog, an eshop page, poll app could all be an app in our newly created project. The examples are endless.
After project creation to create an app you can use
The structure of the polls app should be as below:
All the poll application logic resides here.
We try first by creating a view that return a sample http response. For writing view for your polls application you have to put it inside of
Yes! That's it. We have created view in django. But for calling this view from outside you have to map a route to it. In my app I don't have the file
The next step is to bind the newly defined route in polls to your project urls route. So for now go to
The first url says that give every route that starts with
Now run your project with the command below and see the result:
The final step is to open your browser and point it to
Voila! Good job everybody :)
If you go to base path you will see the error below (see the picture):
#django #url #include #views #pattern #django_part3
After project creation to create an app you can use
startapp
. For now we create a poll app:$ python manage.py startapp polls
$
The structure of the polls app should be as below:
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
All the poll application logic resides here.
We try first by creating a view that return a sample http response. For writing view for your polls application you have to put it inside of
views.py
:from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Yes! That's it. We have created view in django. But for calling this view from outside you have to map a route to it. In my app I don't have the file
urls.py
so create it in polls and map as below:from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
url
is django module to implement the routing functionality.The next step is to bind the newly defined route in polls to your project urls route. So for now go to
saturn/urls.py
:from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
The first url says that give every route that starts with
polls
to polls.urls
. You can see that we have used include
to reference URL configuration. When it reaches /polls
it removes polls
and send the remaining of the URL to polls
.Now run your project with the command below and see the result:
$ python manage.py runserver
The final step is to open your browser and point it to
/polls
and see the result:http://127.0.0.1:8000/polls/
Voila! Good job everybody :)
If you go to base path you will see the error below (see the picture):
#django #url #include #views #pattern #django_part3
To install your python module you need a setup.py file inside of your project. The content of the setup.py can be something like below:
Now if you want to install it on your machine as a package just:
If you want to upload it into your python repository:
NOTE: you should be in the root of your project before running the previous command.
my_repo server should be declared inside of
If you want to install something from your repository via pip you have to provide the
NOTE: you can put inside of an ENV variable in OS to access it better and in more readable way.
#python #setuppy #setup #package #pypi #private_repo #pypirc #pip #install
setup(name=PROJECT_NAME,
version=PROJECT_VERSION,
description=PROJECT_DESCRIPTION,
long_description=PROJECT_LONG_DESCRIPTION,
author='Ali',
author_email='info@ali.com',
url='http://www.ali.com/',
license='SOMETHING',
entry_points=PROJECT_ENTRY_POINTS,
classifiers=[],
keywords='',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
include_package_data=True,
namespace_packages=[],
install_requires=['LIST', 'OF', 'REQUIREMENTS'],
zip_safe=False,
test_suite='nose.collector')
Now if you want to install it on your machine as a package just:
python setup.py install
If you want to upload it into your python repository:
python setup.py sdist upload -r my_repo
NOTE: you should be in the root of your project before running the previous command.
my_repo server should be declared inside of
~/.pypirc
:[distutils]
index-servers =
my_repo
[my_repo]
repository: http://pypi.repo.my_host.com:5000
username: username
password: password
If you want to install something from your repository via pip you have to provide the
extra-index-url
argument:--extra-index-url http://username:password@pypi.repo.my_host.com:5000 --trusted-host pypi.repo.my_host.com
NOTE: you can put inside of an ENV variable in OS to access it better and in more readable way.
#python #setuppy #setup #package #pypi #private_repo #pypirc #pip #install
In this session we'll have a brief look at the
To setup your database you need to give the credential in saturn/settings.py in the section:
As you see above the default database is set to
If you are using a db other than sqlite like
our project. For example for session management there is
Some of these applications use database tables, so for Django apps to work properly, youn need to use
and create corresponding tables:
You can see that needed tables are created as above. In case you don't need an app just comment it out from
The next session we would create models for our tables and would do the real parts. :)
#python #django #settings #INSTALLED_APPS #migration #database #ENGINE #django_part4
settings.py
file and do a migration for the saturn
project.To setup your database you need to give the credential in saturn/settings.py in the section:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
As you see above the default database is set to
sqlite3
and the NAME
of the database is set to file path of db.sqlite3
, if your database is set to MySQL you would have to provide the name of the database in NAME
section.ENGINE
can have one of the below database bindings:'django.db.backends.sqlite3',
'django.db.backends.postgresql',
'django.db.backends.mysql',
'django.db.backends.oracle'
If you are using a db other than sqlite like
MySQL
you should provide USER
, PASSWORD
HOST
beside ENGINE
and NAME
.INSTALLED_APPS
is a section that contains apps used by our project. For example for session management there is
django.contrib.sessions
section.Some of these applications use database tables, so for Django apps to work properly, youn need to use
migrate
commandand create corresponding tables:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
You can see that needed tables are created as above. In case you don't need an app just comment it out from
INSTALLED_APPS
before running migration.The next session we would create models for our tables and would do the real parts. :)
#python #django #settings #INSTALLED_APPS #migration #database #ENGINE #django_part4
Install a specific version of
https://askubuntu.com/questions/957439/how-to-install-a-specific-version-of-node-on-ubuntu-server
#ubuntu #linux #nodejs #node #versioin #installation
NODEJS
on linux server:https://askubuntu.com/questions/957439/how-to-install-a-specific-version-of-node-on-ubuntu-server
#ubuntu #linux #nodejs #node #versioin #installation
Ask Ubuntu
How to install a Specific Version of Node on Ubuntu Server
I am trying to install Node 6.11.3 on my Ubuntu Server.
I don't want to use nvm for this.
I have already used this link but it just does not work out while using jenkins and stuff.
I want to
I don't want to use nvm for this.
I have already used this link but it just does not work out while using jenkins and stuff.
I want to
Install NPM packages from
Sometimes because of filtering issues you need to download node packages from http so make sure to make it http:
#node #npm #shit #installation #config
http
or https
:npm config set registry https://registry.npmjs.org/
Sometimes because of filtering issues you need to download node packages from http so make sure to make it http:
npm config set registry http://registry.npmjs.org/
#node #npm #shit #installation #config
If you have Mac system and created bash scripts so far, there is a fun feature on OS X that you can type
Or let's say you have a bash script that will take a lot of time and at the end of the script your want to hear
#osx #say #command #fun
say
command. say
will say the phrase:say hello channel users
Or let's say you have a bash script that will take a lot of time and at the end of the script your want to hear
operation done successfully
from your speaker:say operation done successfully
#osx #say #command #fun
There is a command in osX called
The great thing about this command is that you can open finder when you have traversed a long path and now you want it to have inside of finder. Go to the desired directory in your terminal using
it will open you finder. :)
#osx #open #command #fun #trick
open
that will open finder for you from within of your terminal.The great thing about this command is that you can open finder when you have traversed a long path and now you want it to have inside of finder. Go to the desired directory in your terminal using
cd
then use:open .
it will open you finder. :)
#osx #open #command #fun #trick
If you get the below error for
To solve this problem, put the below line of code before any
reference: https://github.com/npm/npm/issues/13306
#npm #docker #container #cross_device
cross device link
when installing npm on container:npm WARN optional Skipping failed optional dependency /webpack/watchpack/chokidar/fsevents:
npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.12
npm ERR! Linux 3.16.0-4-amd64
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v6.3.0
npm ERR! npm v3.10.3
npm ERR! path /app/node_modules/istanbul/node_modules/abbrev
npm ERR! code EXDEV
npm ERR! errno -18
npm ERR! syscall rename
npm ERR! EXDEV: cross-device link not permitted, rename '/app/node_modules/istanbul/node_modules/abbrev' -> '/app/node_modules/abbrev'
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR! <https://github.com/npm/npm/issues>
npm ERR! Please include the following file with any support request:
npm ERR! /app/npm-debug.log
The command '/bin/sh -c npm install' returned a non-zero code: 238
To solve this problem, put the below line of code before any
npm
command:RUN cd $(npm root -g)/npm && npm install fs-extra && sed -i -e s/graceful-fs/fs-extra/ -e s/fs.rename/fs.move/ ./lib/utils/rename.js
reference: https://github.com/npm/npm/issues/13306
#npm #docker #container #cross_device
GitHub
EXDEV: cross-device link not permitted, rename... in Docker · Issue #13306 · npm/npm
I'm opening this issue because: npm is crashing. npm WARN optional Skipping failed optional dependency /webpack/watchpack/chokidar/fsevents: npm WARN notsup Not compatible with your operating s...
Today in django tutorial series we define 2 models in
Our model's parent class is django
* The name of each field is the column name in the table.
* max_length can be given as an argument to put restrictions on field's length
* you can give a human readable name to each field by providing an optional first positional argument like pub_date which is set to
* you can define foreign keys by
* fields can have default value is we have set for votes by using
Until now
Now use
Finally use the below command to create the tables:
So we've done 2 steps overall:
1- used
2- applied the migration generated in step 1 by using
#python #django #migrate #models #migrate #makemigrations #django_part5
polls/models.py
. One for Question
and another for Choice
.from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
Our model's parent class is django
models.Model
and each model has its own fields. You can define string field by CharField
, integer by IntegerField
and datetime by DateTimeField
.* The name of each field is the column name in the table.
* max_length can be given as an argument to put restrictions on field's length
* you can give a human readable name to each field by providing an optional first positional argument like pub_date which is set to
date published
* you can define foreign keys by
models.ForeignKey
* fields can have default value is we have set for votes by using
default
Until now
Django
knows nothing about our polls app, we need to plug it in, in order to include it in our project. Go to saturn/settings.py
and do as follow:INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Now use
makemigrations
command to tell Django to store new model changes in migrations
folder inside of polls:$ python manage.py makemigrations polls
Migrations for 'polls':
polls/migrations/0001_initial.py
- Create model Choice
- Create model Question
- Add field question to choice
Finally use the below command to create the tables:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
Applying polls.0001_initial... OK
migrate
is smart and just applies those changes needed, if you are careful enough you can see that a file is created that is called polls.0001_initial
. This file is created by our command makemigrations
in the previous step.So we've done 2 steps overall:
1- used
makemigrations
to create migration files.2- applied the migration generated in step 1 by using
migrate
.#python #django #migrate #models #migrate #makemigrations #django_part5
Django has an admin panel that does lots of tedious works that if you had to create yourself, it wouldn't be an easy t
ask. In this part we try to build the admin panel from ground up.
First of all an admin panel needs an admin user. So create one first:
Ok after creating the admin user, run the developement server:
Now point your URL to
e administrator page of Django.
#python #django #admin #dashboard #createsuperuser #django_part6
ask. In this part we try to build the admin panel from ground up.
First of all an admin panel needs an admin user. So create one first:
$ python manage.py createsuperuser
Username (leave blank to use 'fc'): alireza
Email address: a***@***.com
Password:
Password (again):
Superuser created successfully.
Ok after creating the admin user, run the developement server:
$ python manage.py runserver
Now point your URL to
http://127.0.0.1:8000/admin/
. You should see the login page in the next post. Login and see the administrator page of Django.
#python #django #admin #dashboard #createsuperuser #django_part6
Tech C**P
Django has an admin panel that does lots of tedious works that if you had to create yourself, it wouldn't be an easy t ask. In this part we try to build the admin panel from ground up. First of all an admin panel needs an admin user. So create one first:…
Django has an admin panel that does lots of tedious works that if you had to create yourself, it wouldn't be an easy task. In this part we try to build the admin panel from ground up.
First of all an admin panel needs an admin user. So create one first:
Ok after creating the admin user, run the developement server:
Now point your URL to
#python #django #admin #dashboard #createsuperuser #django_part6
Now to see polls in admin page, you need to register you models. open
Last 2 lines are added to register the
First of all an admin panel needs an admin user. So create one first:
$ python manage.py createsuperuser
Username (leave blank to use 'fc'): alireza
Email address: a***@***.com
Password:
Password (again):
Superuser created successfully.
Ok after creating the admin user, run the developement server:
$ python manage.py runserver
Now point your URL to
http://127.0.0.1:8000/admin/
. You should see the login page in the next post. Login and see the administrator page of Django.#python #django #admin #dashboard #createsuperuser #django_part6
Now to see polls in admin page, you need to register you models. open
polls/admin.py
:# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from .models import Question
admin.site.register(Question)
Last 2 lines are added to register the
Question
model as picture below: