Get only the text from the Django form.

The hierarchy is as follows.

mysite(startproject)
  |__setting.py
  |__urls.py
  |__model.py
manage.py
db.sqlite3
blog(app)
  |__views.py
  |__urls.py 
  |__wsgi.py

Write the source code in views.py. You can get the text in the form ** with ** form.cleaned_data **. At this stage, it is ** dictionary type ** and the key is text, so you can get it with ** form.cleaned_data ['text'] **.

blog/views.py


def article_edit(request, pk):
    post = get_object_or_404(Article, pk=pk)
    if request.method == "POST": 
        form = ArticleForm(request.POST, instance=post)
        if form.is_valid():
                #Display debug messages on the console using logging.
                logging.debug(form.cleaned_data['text'])
                post = form.save(commit=False)
                post.author = request.user
                post.published_date = timezone.now()
                post.save()
                return redirect('article_detail', pk = post.pk)
     else:
        form = ArticleForm(instance=post)
    return render(request, 'blog/article_edit.html', {'form' : form})

If you cannot display the debug message, refer to the following.

How to print debug messages to the Django console http://qiita.com/NoriakiOshita/items/7716c6e46338768467eb

Recommended Posts

Get only the text from the Django form.
Get the value from the [Django] Form
DJango Note: From the beginning (form processing)
DJango Memo: From the beginning (preparation)
[Beginner] Get from Django Query database
Get the address from the zip code
Get the query string (query string) in Django
Get the client's IP address in Django
DJango Memo: From the beginning (model settings)
Get the address from latitude and longitude
Get wordpress posts from the past week
[Python] Get the main color from the screenshot
Get news from three major mobile companies using Django and the News API
Get the contents of git diff from python
The easiest way to get started with Django
Django Contact Form 2
DJango Memo: From the beginning (creating a view)
Get only the subclass elements in a list
[Django] Create a form that automatically fills in the address from the zip code
Get only the source code of the PyPI package with pip from the command line
Get only the address part of NIC (eth0)
Extract only complete from the result of Trinity
Django Form Gleaning
DJango Memo: From the beginning (Error screen settings)
Get only articles from web pages in Python
Django contact form
The wall of changing the Django service from Python 2.7 to Python 3
Get the return code of the Python script from bat
Set the form DateField to type = date in Django
[2020 version] Scraping and processing the text from Aozora Bunko
DJango Note: From the beginning (using a generic view)
DJango Note: From the beginning (creating a view from a template)
[Blender] Use the text drawing module from within the script
[Django] Give Form a dynamic initial value from Model
Get the GNOME version
Django memo # 1 from scratch
Extracted text from image
[Django] Rename the project
How to get only the data you need from a structured data set using a versatile method
Create a data frame from the acquired boat race text data
Get your heart rate from the fitbit API in Python!
Get the package version to register with PyPI from Git
The story of a Django model field disappearing from a class
Extract only the cat part from the cat image (matting / semantic segmentation)
Get the value while specifying the default value from dict in Python
How to do the initial setup from Django project creation
Django Make choices only for the facility you belong to
DJango Memo: From the beginning (more edits to the management screen)
I tried to get various information from the codeforces API