2017年11月30日木曜日

DjangoのModel使用

1)Modelを定義
appフォルダーにmodels.pyにTableとColumnを定義
from django.db import models

# Create your models here.
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)

2)appをProjectに結び付く
settings.pyのINSTALLED_APPSにappを追加

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    .....

3)migrations作成
python manage.py makemigrations polls

4)migrate実施
python manage.py sqlmigrate polls 0001  #どんなSQL実行されるか確認
python manage.py migrate

Django appを作成

1)Projectを作成、cloud9環境ではすでに作成されたので、不要
django-admin startproject mysite

2)appを作成
python manage.py startapp polls

Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

3)Viewを作成
polls/views.py を編集

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

4)To call the view, we need to map it to a URL
 polls/urls.py を作成

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

5)To point the root URLconf at the polls.urls module

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),
]

6)DBの設定
MySQLのアクセスモジュールを追加
sudo pip3 install PyMySQL

Projectフォルダーにsettings.pyのDATABASESを変更
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'c9',  #dbname
        'USER': 'XXXXc9',       #username
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
    }
}

manage.pyに以下を追加
import pymysql
pymysql.install_as_MySQLdb()

7)migrate実施
python manage.py migrate
settings.pyのINSTALLED_APPSに必要なTableが作成されれば、DB設定OK


2017年10月26日木曜日

cloud9でpython3/djangoを設定

django projectの設定(歯車)から、python2->3にしてから以下を実施

sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3.4 /usr/bin/python  ->ここ3.5を設定したら、djangoが認識してくれなかったとりあえず3.4にした
sudo pip3 install django

以下のコマンドでバージョン確認
python -V
python -m django --version

2017年10月6日金曜日

macにphpmyadminをインストール

brew install phpmyadmin

sudo vi /etc/apache2/httpd.conf  で以下の設定を最後に追加

  Alias /phpmyadmin /usr/local/share/phpmyadmin
  <Directory /usr/local/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
      Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
      Order allow,deny
      Allow from all
    </IfModule>
  </Directory>

sudo apachectl restart

参考:https://soarcode.jp/posts/55

2017年9月26日火曜日

cloud9で.netを使用

cloud9にBlankのworkspaceを作成してから、shellで以下のコマンドを実行。


curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-trusty-prod trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
sudo apt-get update
sudo apt-get install dotnet-sdk-2.0.0

確認するには、dotnetの情報を表示。
dotnet --info

Web APPを作成
dotnet new razor -o app
或いは、
mkdir app
cd app
dotnet new mvc

cloud9にアクセスできるように、Program.csを変更

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseUrls("http://0.0.0.0:8080") //この行を追加
                .Build();

変更後のBuild
cd app
dotnet build
dotnet run

そして、通常のcloud9 app URLにアクセスすればOK

参考:https://www.youtube.com/watch?v=ioSeIpphmAA

2017年8月16日水曜日

Laravelにおけるviewに変数を渡す方法

viewのwelcomeに変数$nameを渡すには、以下の方法がある

1)
Route::get('/', function () {
    return view('welcome', ['name' => 'World',
    ]);
});

2)
Route::get('/', function () {
    return view('welcome')->with('name', 'World');
});

3)
Route::get('/', function () {
 $name = 'World';
    $age = 42;

    return view('welcome', ['name' => $name,
                                           'age' => $age,
    ]);
    //same with, return view('welcome',  compact('name', 'age'));
});

Route::get('/', function () {
    $tasks = [
        'qwe',
        'asd',
        'zxc'
    ];

    return view('welcome', compact('tasks'));
});

shellの$()

tplList.txtに保存するfile listを対象にgrepしたい。

grep -i "href" $(cat tplList.txt)
$()の意味は``と同じ、実行結果をgrepに渡す