[PYTHON] I touched Wagtail (2). Introducing django-extensions.

Introduction

Hello.

I would like to proceed with development using Wagtail, but at present, no libraries required for debugging etc. are installed. The Django libraries you can use for debugging are django-extensions or django-debug-toolbar. .readthedocs.io/en/latest/) and so on. ~~ It seems that only that is known ~~

So this time, I introduced django-extensions, so I summarized the flow. I'd also like to investigate the Django library.

Previous article


Introducing django-extensions

I will write about the flow of introducing django-extensions. I did it on mac.

First, execute the following command.

pip install django-extensions

Next, add django-extensions to INSTALLED_APPS in mysite / settings / base.py.

You can use the basic functions with this alone, but there are some that cannot be used. There is a command that can generate an ER diagram of the model (discussed below), but it required some steps.

brew install Graphviz
pip install pygraphviz

I got an error and it didn't work, but I was able to solve it by looking at this ISSUE. Reference: Cannot install pygraphviz on Mac OS 10.11.6


Try django-extensions

□show_urls The django-extensions feature is pretty handy. The first one to introduce is show_urls.

python manage.py show_urls

The relationships in the "View" and "template" of the URLs set in the Django project are displayed in a list. I think this expression is difficult to understand, so let's first look at the execution log. (Excerpt)

/django-admin/login/    django.contrib.admin.sites.login        admin:login                                                                                                                                 
/django-admin/logout/   django.contrib.admin.sites.logout       admin:logout                                                                                                                                
/django-admin/password_change/  django.contrib.admin.sites.password_change      admin:password_change                                                                                                       
/django-admin/password_change/done/     django.contrib.admin.sites.password_change_done admin:password_change_done 

On the left side, / django-admin / login / etc. http://localhost:8000/ Represents the path (URL pattern) that follows.

Django.contrib.admin.sites.login in the center shows the location of the view function. Take a look at django.contirib.admin.sites on django's Github. You can see that there is a login function.

The admin: login on the right side represents the name of the path. When setting the URL in url_patterns, set it as the third argument

name="hogehoge"

You write it like this. That name corresponds to this.

As a test, I would like to add one view function and examine it using show_urls. I will write the code based on the reference site. Reference: [Introduction to Django] Let's display the page using views

The added code is as follows.

home/views.py


from django.http import HttpResponse
 
def index(request):
    return HttpResponse("Hello, world.")

home/urls.py



from django.conf.urls import url
from . import views
 
urlpatterns = [
    url(r'^$', views.index, name='index'),
]

mysite/urls.py


from home import views as home_views #Added at the top

url(r'^home/', include('home.urls')) #Added in the list of urlpatterns

Click [here](http: // localhost: 8000 / home /) and when Hello, world. Is displayed, it is OK.

Now, let's execute the following command.

python manage.py show_urls | grep home
/home/  home.views.index        index

It was displayed. This is useful when importing views or entering URLs in templates. Reference: How to paste URL links for Django apps and their benefits

** □ ER diagram ** Reference: ER Diagram (Entity Relationship Diagram) With django-extensions, you can generate an ER diagram of your project in png with a single command. It is an excellent one that can be used when designing a database. Now, let's execute the command and check it.

python manage.py graph_models -a -o mysite_er.png

I think mysite_er.png is generated in the same directory. er.png ER diagram

□runserver_plus I would like to introduce another one this time. It's great for debugging on a web browser. Let's execute the command immediately.

python manage.py runserver_plus

Then ...

CommandError: Werkzeug is required to use runserver_plus.  Please visit http://werkzeug.pocoo.org/ or install via pip. (pip install Werkzeug)

** Install Wekzeug as you are told and try again. ** **

It's different from a normal runserver. There is something like this on the last line of the log. Make a note of this as you will use it later.

 * Debugger PIN: xxx-xxx-xxx

Now, let's edit the code as follows.

home/views.py


return HttpReesponse("Hello, world.") #Make a mistake on purpose
スクリーンショット 2019-12-24 19.30.26.png Error screen スクリーンショット 2019-12-24 19.31.24.png Error screen bottom

There is a terminal-like button on the right side. You can debug by clicking on it and entering the PIN code you wrote down earlier. Since Python code can be executed interactively, you can check the values of objects such as variables.


at the end

This time, I introduced django-extensions to my project. Next time I would like to touch Wagtail.

Recommended Posts

I touched Wagtail (2). Introducing django-extensions.
I touched HaikuFinder
I touched Flask
I touched TensorFlow's Tensorboard
I touched AWS Chalice
I touched Wagtail (3). Investigation and implementation of pop-up messages.
I touched Wagtail (1) and let's override the save method.
I touched the Qiita API
I touched Tensorflow and keras
I touched PyAutoIt for a moment
I touched something called Touch Designer