action in a form

17 views
Skip to first unread message

bengoshi

unread,
Jun 23, 2019, 3:57:39 PM6/23/19
to Django users
Hi,
I tried to write a form:

###
{% extends 'base.html' %}
{% block content %}
<h1>Test</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save'/>
</form>
{% endblock %}
###

and it works. But if I replace it with

<form action="journal" method="POST"

"journal" is a view for shown the saved data which work, too. If I click to "Save" it change to "journal" but won't save the data anymoren.

Could anybody explain me please this behavior?

Thanks and Greetings

bengoshi


Sebastian Jung

unread,
Jun 23, 2019, 4:47:32 PM6/23/19
to django...@googlegroups.com
Hello,

You must Put in Action not a Name from a function. You must Put a url dir example action="/newurl/"

And in your url a Link from /newurl/ to the function

Regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e153dd08-3678-4620-8f1d-dae1fa1b978f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Lutalo Bbosa joseph

unread,
Jun 23, 2019, 5:29:27 PM6/23/19
to django...@googlegroups.com
as jung suggests action is meant to take in  a url forexample action =" {% url "cart:journal" %}", where cart is name of the cart and journal is the
name of the url

Sipum Mishra

unread,
Jun 23, 2019, 5:31:05 PM6/23/19
to django...@googlegroups.com
Hello,

In action use the url associated with that view then it will work for you. 
If not then kindly tell what errors you are getting. 

Thanks. 

bengoshi

unread,
Jun 23, 2019, 8:36:14 PM6/23/19
to Django users
Thanks for your responses. I didn't describe it well.. if I write
<form action="journal" method="POST">
it calls the url journal and this is the my requested result. All fine.
Without the attribute "action" the form save the input in the database - the requested result, great.
With "action" it doesn't. That is the point which I don't understand becauce in my understanding
action should not have any effect for saving data.

Greetings
bengoshi

Am Sonntag, 23. Juni 2019 19:31:05 UTC+2 schrieb Sipum:
Hello,

In action use the url associated with that view then it will work for you. 
If not then kindly tell what errors you are getting. 

Thanks. 

On Sun, 23 Jun, 2019, 10:17 PM Sebastian Jung, <sebasti...@gmail.com> wrote:
Hello,

You must Put in Action not a Name from a function. You must Put a url dir example action="/newurl/"

And in your url a Link from /newurl/ to the function

Regards

bengoshi <kai.kob...@gmail.com> schrieb am So., 23. Juni 2019, 17:57:
Hi,
I tried to write a form:

###
{% extends 'base.html' %}
{% block content %}
<h1>Test</h1>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save'/>
</form>
{% endblock %}
###

and it works. But if I replace it with

<form action="journal" method="POST"

"journal" is a view for shown the saved data which work, too. If I click to "Save" it change to "journal" but won't save the data anymoren.

Could anybody explain me please this behavior?

Thanks and Greetings

bengoshi


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Sipum Mishra

unread,
Jun 24, 2019, 6:45:39 AM6/24/19
to django...@googlegroups.com
Hi bengoshi,

If i m not wrong, your concern is when you add action ='journal' then data are not saved. 
Right? 

Thanks. 


To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Kai Kobschätzki

unread,
Jun 24, 2019, 6:49:38 AM6/24/19
to django...@googlegroups.com
Hi Sipum,
yes, you are right!

Best Greetings

bengoshi


For more options, visit https://groups.google.com/d/optout.


--

Sebastian Jung

unread,
Jun 24, 2019, 7:17:13 AM6/24/19
to django...@googlegroups.com
Hello,

Please Post us your url.py template and the related function from view.py

Regards

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Kai Kobschätzki

unread,
Jun 24, 2019, 7:14:56 PM6/24/19
to django...@googlegroups.com

Hi Sebastian,

here is the code:

urls.py:

from django.contrib import admin
from django.urls import path
from rewe.views import account_overview_view, journal_booking_view, journal_overview_view
from crm.views import member_overview_view, member_create_view

urlpatterns = [
    path('', journal_overview_view),
    path('overview_account', account_overview_view),
    path('overview_member', member_overview_view),
    path('member_create', member_create_view, name='member_create'),
    path('booking', journal_booking_view),
    path('journal', journal_overview_view),
    path('admin/', admin.site.urls),
]

views.py

from .models import Account, Journal
from crm.models import Member
from .forms import BookingForm


def journal_booking_view(request):
    form = BookingForm(request.POST or None)
    if form.is_valid():
        form.save()
        form = BookingForm()
    context = {
        'form' : form
    }
    return render(request, "rewe/rewe_journal_booking.html", context)



def journal_overview_view(request):
    journal_entrys = Journal.objects.all()
    context = {
        'journal'       : journal_entrys
        }
    return render(request, "rewe/rewe_journal_overview.html", context)


forms.py

from django import forms
from .models import Journal

class BookingForm(forms.ModelForm):
    class Meta:
        model = Journal
        fields = [
            'amount',
            'date',
            'posting_text'
        ]



Thanks and Greetings

bengoshi



Sebastian Jung

unread,
Jun 24, 2019, 8:41:03 PM6/24/19
to django...@googlegroups.com
You send your Form to function journal_overview_view. There are nothing with save()

Regards

Kai Kobschätzki

unread,
Jun 24, 2019, 8:55:17 PM6/24/19
to django...@googlegroups.com

Well, perhaps there is my misunderstanding: I thought if I press "save" it pass through the functions journal_booking_view and then it call the action-argument. Otherwise there is no action-argument it calls the journal_booking_view for saving the view. I'm confused.

Greetings

bengoshi

Reply all
Reply to author
Forward
0 new messages