[Python] Beginners troubleshoot while studying Django web applications

1. About this article

Overview

I made a reminder of what I got caught while studying Django's web application development.

Contents

I studied according to this tutorial. https://eiry.bitbucket.io/

※※ Since the content is "Beginners are ...", it may not sound for those who have become familiar with Django and other web applications to some extent.

2. Tools / environment used

environment ・ Windows 10 ・ Python 3.8.0 ・ Web framework: django 2.2.5 ・ DB: sqlite3.30.1

tool ・ Pycharm


3. Troubleshooting

(A) The text color in the __HTML input tag and textarea tag becomes white.

(1) Phenomenon

Settings

Specify the forms in forms.py as follows

forms.py


from django import forms

from .models import Posting


class PostingForm(forms.ModelForm):
    class Meta:
        model = Posting
        fields = ('name', 'message')
        widgets = {
            'name': forms.TextInput(attrs={'size': 40, 'required': True}),
            'message': forms.Textarea(attrs={'cols': 80, 'rows': 20, 'required': False})
        }

Pass the form information to index.html in views.py as follows.

Part of views



def index(request):
    """Process display / post"""
    #ModelForm and Form are used in the same way when creating an instance
    form = PostingForm(request.POST or None)
    if request.method == 'POST':
        #False if None
        if form.is_valid():
            # save()Just call the method and it will be registered in the DB using Model.
            form.save()
            #Use the message framework to notify the user that the process was successful
            messages.success(request, 'The post has been accepted.')
            return redirect('guestboard:index')
        else:
            #Use the message framework to notify the user that the process has failed
            messages.error(request, 'There is an error in the input content.')
    page = _get_page(
        Posting.objects.order_by('-id'),  #Get posts sorted in chronological order
        request.GET.get('page'),  #Get page number from GET query
        count=3
    )
    contexts = {
        'form': form,
        'page': page,
    }
    return render(request, 'guestboard/index.html', contexts)

The contents of index.html are like this. Expand the data on the form with {{field.label_tag}} and {{field}} respectively.

Part of index


    <form action="{% url 'guestboard:index' %}" method="post">
        <div class="row">
            {% for field in form %}
                <div class="form-group">
                    {% if field.errors %}
                        <div class="col-sm-10">
                            <span class="col-sm-10">{{ field.errors }}</span>
                        </div>
                    {% endif %}
                    <div class="col-sm-10 ">
                        <label class="col-sm-3 control-label ">{{ field.label_tag }}</label>
                        <label class="label col-sm-7 ">{{ field }}</label>
                    </div>
                </div>
            {% endfor %}
            <div class="col-sm-10">
                <div class="form-group">
                    <label class="col-sm-2 control-label"><input type="submit" class="btn btn-primary"
                                                                 value="Registration"></label>
                    {% csrf_token %}
                </div>
            </div>
        </div>
    </form>

css uses bootstrap 3.3.5 and a self-made sheet.

        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <link rel="stylesheet" href="{% static 'css/common.css' %}">

Trouble

Try setting up a test server. ** Then, the characters in the input box will turn white and become invisible as shown below. ** **

Below, I'm writing in the name and message fields. コメント 2020-01-19 145606.png

Inverted and finally visible コメント 2020-01-19 145620.png

(2) Conclusion

First of all, the following two points.

-The default setting of bootstrap 3.3.5 was bad (?). ・ After changing my own CSS, "Super Reload" was required.

(3) Troubleshooting

3-1 First, I opened the chrome developer tools and investigated.

コメント 2020-01-19 151440.png

Watch the style sheet column.

コメント 2020-01-19 151549.png

Then, I was able to confirm that the default settings for the bootstrap textarea were applied. Characters appear when this is turned off for verification. コメント 2020-01-19 151813.png

So, I decided to overwrite it with my own CSS file.

3-2 Defined as follows in Django's static file storage folder. Defined text_change_color selector so that it can be applied to child elements.

common.Part of css



.text_change_color {
    color: black !important;
}

.text_change_color * {
    color: black !important;
}

Modified index.html.

index.Part of html


                    <div class="col-sm-10 text_change_color">
                        <label class="col-sm-3 control-label ">{{ field.label_tag }}</label>
                        <label class="label col-sm-7 ">{{ field }}</label>
                    </div>

I launched a web application with this, The letters were still white.

コメント 2020-01-19 145606.png

3-3 I was suspicious and looked at the contents of css with the developer tools, The changes have not been reflected. コメント 2020-01-19 152507.png

Rebooting the app doesn't change the situation.

After a lot of research, I found that I had to do a "super reload" to initialize the cache. Reference: https://qiita.com/shati-taro/items/3946d3962071a26ebcb6

** When I did a super reload, it was reflected safely and I was able to confirm that the characters in the input box turned black properly. ** **


(B) The time value of the DB registered with __auto_now_add does not reflect TIMEZONE

~~ ### (1) Phenomenon ~~ ~~ #### Settings ~~ ~~ Define the Posting model as follows. ~~ ~~ To specify the registration date and time, set the "auto_now_add" argument of DateTimeField to True ~~ ~~ Set the current system date and time to be registered. ~~



#### **`models.Part of py`**
```py

from django.db import models


class Posting(models.Model):
    name = models.CharField(
        max_length=64,
        verbose_name='name',
        help_text='<b>Please input your name</b>',
    )
    message = models.CharField(
        max_length=255,
        verbose_name='message',
        help_text='<u>Please enter a message</u>',
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Registered Date',
    )

```



 ~~ Also, specify TIME ZONE as Asia / Tokyo (Japan Standard Time) in setting, py. ~~
 TIMEZONE enabled as ~~ USE_TZ = True. ~~

TIME_ZONE = 'Asia/Tokyo'

USE_I18N = True

USE_L10N = True

USE_TZ = True

When you press the register button on the ~~ index page, each model data is registered as a record in the DB. ~~

 ~~ #### Trouble ~~
 ~~ Set up a test server under the above conditions ~~
 ~~ In my PC time [around 2020-01-18 17:35] ~~
 ~~ Type the message "767675467" ~~
 ~~ Press the registration button on the screen. ~~

~~ To confirm that the data has been registered ~~ ~~ Open the database tool window on Pycharm ~~ ~~ Check the contents. ~~

 ~~ ** Then, it is registered in UTC time. ** ~~

~~![Comment 2020-01-19 155649.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/541790/9f2cd4b5-c8d8-0b5d-0d79-d08c5cd6ea99 .png) ~~

 ~~ TIMEZONE should be enabled, why? ?? ~~
 ~~ (I'm pretty worried that the column name is garbled) ~~

~~ ### (2) Conclusion ~~ ~~ In conclusion ~~ It was Django's specification for model data generation when ~~ auto_now_add was specified. ~~ ~~ The time zone and so on will be handled by Django. ~~

 ~~ ### (3) Troubleshooting ~~
~~####3-1~~
 ~~ For the time being, I checked whether the data on the screen was consistent with the registration date and time of the database. ~~
 ~~ Then, it can be displayed in Japan Standard Time on the screen. ~~

~~![Comment 2020-01-19 160228.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/541790/34ec29c9-3849-457a-5b0b-d157531506bf .png) ~~

 ~~ Apparently you need to read the code in the DateTimeField. ~~

~~####3-2~~ The constructor of ~~ DateField (parent class of DateTimeField) looks like this. ~~



#### **`python`**
```py


class DateField(DateTimeCheckMixin, Field):
    empty_strings_allowed = False
    default_error_messages = {
        'invalid': _("'%(value)s' value has an invalid date format. It must be "
                     "in YYYY-MM-DD format."),
        'invalid_date': _("'%(value)s' value has the correct format (YYYY-MM-DD) "
                          "but it is an invalid date."),
    }
    description = _("Date (without time)")

    def __init__(self, verbose_name=None, name=None, auto_now=False,
                 auto_now_add=False, **kwargs):
        self.auto_now, self.auto_now_add = auto_now, auto_now_add
        if auto_now or auto_now_add:
            kwargs['editable'] = False
            kwargs['blank'] = True
        super().__init__(verbose_name, name, **kwargs)
```

 If ~~ auto_now_add is True, it seems that editable is passed to the parent class as False. ~~
 ~~ I didn't seem to get any useful information even if I looked here ~~
 I looked at the ~~ pre_save method (the method that is executed when the model is saved). ~~



#### **`pre_save`**
```py

    def pre_save(self, model_instance, add):
        if self.auto_now or (self.auto_now_add and add):
            value = timezone.now()
            setattr(model_instance, self.attname, value)
            return value
        else:
            return super().pre_save(model_instance, add)

```



 When ~~ auto_now_add is True, timezone.now () is called. ~~
 ~~ The contents are ... ~~


```py
def now():
    """
    Return an aware or naive datetime.datetime, depending on settings.USE_TZ.
    """
    if settings.USE_TZ:
        # timeit shows that datetime.now(tz=utc) is 24% slower
        return datetime.utcnow().replace(tzinfo=utc)
    else:
        return datetime.now()

```


 ~~ Whether or not the specified timezone usage of settings.py is valid or not ~~
 The value to return ~~ seems to be in UTC. ~~

~~ Hmm? ~~ ~~ But on the screen, Japan Standard Time can be obtained. ~~ ~~ Why? ~~

~~####3-3~~
 ~~ I couldn't handle it, so I looked it up ~~
 ~~ I found the following article. ~~

~~https://djangobrothers.com/blogs/django_datetime_localtime/~~

 ~~> One thing to keep in mind when using Django's django.utils.timezone is that it's not yet localized when using datetime in your code. ~~
 ~~ It's a little confusing, but for example, until it's displayed in the template, it's dealing with a UTC-based aware datetime object, which is converted to the appropriate local time when it's displayed. .. ~~

~~ If you comply with this specification, you can understand the above behavior. ~~

 ~~ It seems that Django is automatically converting the TIMEZONE when outputting to the screen. ~~
 ~~ It's unclear before what part of the source code this is. ~~


 <font color = "Red"> The official document says that it is registered in the DB with UTC </ font>
 <font color = "Red"> I should have read that first </ font>
[https://docs.djangoproject.com/ja/2.1/topics/i18n/timezones/](https://docs.djangoproject.com/ja/2.1/topics/i18n/timezones/)



# 5. At the end

 This is the path to solving problems for beginners.
 Please comment if you have any supplements.
 Also, if there is something that leaks personal information,
 I would appreciate it if you could tell me a little. (Mailto: [email protected])




Recommended Posts

[Python] Beginners troubleshoot while studying Django web applications
Django python web framework
Python beginners publish web applications using machine learning [Part 1] Introduction
Python Exercise for Beginners # 2 [for Statement / While Statement]
Getting Started with Python Web Applications
Beginners use Python for web scraping (1)
Beginners use Python for web scraping (4) ―― 1
Migrate Django applications running on Python 2.7 to Python 3.5
Studying python
Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
Python beginners publish web applications using machine learning [Part 2] Introduction to explosive Python !!
Easy-to-understand explanation of Python Web application (Django) even for beginners (1) [Environment construction]
[For beginners] Try web scraping with Python
Until you publish a web service on GCP while studying JQuery and Python
The first web app created by Python beginners
Python web framework Django vs Pyramid vs Flask December 2015
What Python beginners got hooked on with Django
Python Django Tutorial (5)
Python Django Tutorial (2)
[Python] Start studying
Python Django Tutorial (8)
Python Django Tutorial (6)
Beginners practice Python
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
Python beginner's note
Python Django Tutorial (3)
Easy-to-understand explanation of Python web application (Django) even for beginners (6) [MTV design pattern completion]
Python Django Tutorial (4)
Deploy a Python 3.6 / Django / Postgres web app on Azure
Beginners can use Python for web scraping (1) Improved version
Python beginners try adding basic auth to Django admin
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 7-
(Python) Try to develop a web application using Django
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 1-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 2-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 0-
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 5-
Quick web scraping with Python (while supporting JavaScript loading)
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 6-
Web application made with Python3.4 + Django (Part.1 Environment construction)
Python beginners get stuck with their first web scraping
[Beginner] [Python / Django] A fledgling web engineer tried a Django tutorial-Part 3-
Beginners use Python for web scraping (4) --2 Scraping on Cloud Shell