Post and save Django variable, doesn´t work
could someone please check how should I write correctly the views.py part?
If I code:
from django.shortcuts import render, redirect
def shifts_table(request):
print(request.POST['value'])
return render(request, 'shifts_table.html', {})
...at least the page runs, but if I code like below it doesn't, any idea why?
from django.shortcuts import render, redirect
from django.contrib import messages
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
if number.is_valid():
number.save()
return redirect('shifts_table.html')
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
home.html:
<form action="{% url 'shifts_table' %}" method='POST'>
{% csrf_token %}
<label for='number'>Number:</label>
<input type="number" name="value" placeholder="2020" required><br/>
<button type="submit">submit</button>
</form>
urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('shifts_table', views.shifts_table, name='shifts_table'),
]
python django python-3.x django-templates django-views
add a comment |
could someone please check how should I write correctly the views.py part?
If I code:
from django.shortcuts import render, redirect
def shifts_table(request):
print(request.POST['value'])
return render(request, 'shifts_table.html', {})
...at least the page runs, but if I code like below it doesn't, any idea why?
from django.shortcuts import render, redirect
from django.contrib import messages
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
if number.is_valid():
number.save()
return redirect('shifts_table.html')
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
home.html:
<form action="{% url 'shifts_table' %}" method='POST'>
{% csrf_token %}
<label for='number'>Number:</label>
<input type="number" name="value" placeholder="2020" required><br/>
<button type="submit">submit</button>
</form>
urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('shifts_table', views.shifts_table, name='shifts_table'),
]
python django python-3.x django-templates django-views
but if I code like below it doesn't - what does happen when you run that code?
– Alasdair
Nov 26 '18 at 22:42
ERR_CONNECTION_REFUSED .... and like I commented to our colleague Amine, I would like to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without creating objects and models?
– kubaSpolsky
Nov 27 '18 at 8:29
You can’t set a variable in one request and access it in another. You could save data in the session, or a cookie, or a cache, then refetch it in another view.
– Alasdair
Nov 27 '18 at 8:51
add a comment |
could someone please check how should I write correctly the views.py part?
If I code:
from django.shortcuts import render, redirect
def shifts_table(request):
print(request.POST['value'])
return render(request, 'shifts_table.html', {})
...at least the page runs, but if I code like below it doesn't, any idea why?
from django.shortcuts import render, redirect
from django.contrib import messages
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
if number.is_valid():
number.save()
return redirect('shifts_table.html')
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
home.html:
<form action="{% url 'shifts_table' %}" method='POST'>
{% csrf_token %}
<label for='number'>Number:</label>
<input type="number" name="value" placeholder="2020" required><br/>
<button type="submit">submit</button>
</form>
urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('shifts_table', views.shifts_table, name='shifts_table'),
]
python django python-3.x django-templates django-views
could someone please check how should I write correctly the views.py part?
If I code:
from django.shortcuts import render, redirect
def shifts_table(request):
print(request.POST['value'])
return render(request, 'shifts_table.html', {})
...at least the page runs, but if I code like below it doesn't, any idea why?
from django.shortcuts import render, redirect
from django.contrib import messages
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
if number.is_valid():
number.save()
return redirect('shifts_table.html')
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
home.html:
<form action="{% url 'shifts_table' %}" method='POST'>
{% csrf_token %}
<label for='number'>Number:</label>
<input type="number" name="value" placeholder="2020" required><br/>
<button type="submit">submit</button>
</form>
urls.py:
urlpatterns = [
path('', views.home, name='home'),
path('shifts_table', views.shifts_table, name='shifts_table'),
]
python django python-3.x django-templates django-views
python django python-3.x django-templates django-views
edited Nov 26 '18 at 22:43
Alasdair
182k26308310
182k26308310
asked Nov 26 '18 at 22:36
kubaSpolskykubaSpolsky
164
164
but if I code like below it doesn't - what does happen when you run that code?
– Alasdair
Nov 26 '18 at 22:42
ERR_CONNECTION_REFUSED .... and like I commented to our colleague Amine, I would like to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without creating objects and models?
– kubaSpolsky
Nov 27 '18 at 8:29
You can’t set a variable in one request and access it in another. You could save data in the session, or a cookie, or a cache, then refetch it in another view.
– Alasdair
Nov 27 '18 at 8:51
add a comment |
but if I code like below it doesn't - what does happen when you run that code?
– Alasdair
Nov 26 '18 at 22:42
ERR_CONNECTION_REFUSED .... and like I commented to our colleague Amine, I would like to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without creating objects and models?
– kubaSpolsky
Nov 27 '18 at 8:29
You can’t set a variable in one request and access it in another. You could save data in the session, or a cookie, or a cache, then refetch it in another view.
– Alasdair
Nov 27 '18 at 8:51
but if I code like below it doesn't - what does happen when you run that code?
– Alasdair
Nov 26 '18 at 22:42
but if I code like below it doesn't - what does happen when you run that code?
– Alasdair
Nov 26 '18 at 22:42
ERR_CONNECTION_REFUSED .... and like I commented to our colleague Amine, I would like to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without creating objects and models?
– kubaSpolsky
Nov 27 '18 at 8:29
ERR_CONNECTION_REFUSED .... and like I commented to our colleague Amine, I would like to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without creating objects and models?
– kubaSpolsky
Nov 27 '18 at 8:29
You can’t set a variable in one request and access it in another. You could save data in the session, or a cookie, or a cache, then refetch it in another view.
– Alasdair
Nov 27 '18 at 8:51
You can’t set a variable in one request and access it in another. You could save data in the session, or a cookie, or a cache, then refetch it in another view.
– Alasdair
Nov 27 '18 at 8:51
add a comment |
2 Answers
2
active
oldest
votes
To save a variable you can simply use sessions
request.session['number'] = number
Then you can use request.session['number'] like any other variable
add a comment |
number = request.POST['value']
This will return a string and strings don't have a save method so it will not work
I think that you have a model that is referenced by that number and to save it to the database you can do something like this
object= yourmodel.objects.get(pk=number)
object.save()
Also strings don't have a definition for is_valid(), which is used in forms, which are not implemented in your view
After saving to the database you have to redirect, which is done only if nothing is submitted
Final code :
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
# check if number is valid (not empty and no spaces before or after)
if not number.strip():
object = yourmodel.objects.get(pk=number)
object.save()
return render(request, 'home.html', {})
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
1
Hi Amine, I just want to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without objects and models?
– kubaSpolsky
Nov 27 '18 at 8:27
Please see my other answer
– Amine Messaoudi
Nov 27 '18 at 9:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490168%2fpost-and-save-django-variable-doesn%25c2%25b4t-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To save a variable you can simply use sessions
request.session['number'] = number
Then you can use request.session['number'] like any other variable
add a comment |
To save a variable you can simply use sessions
request.session['number'] = number
Then you can use request.session['number'] like any other variable
add a comment |
To save a variable you can simply use sessions
request.session['number'] = number
Then you can use request.session['number'] like any other variable
To save a variable you can simply use sessions
request.session['number'] = number
Then you can use request.session['number'] like any other variable
answered Nov 27 '18 at 9:46
Amine MessaoudiAmine Messaoudi
526514
526514
add a comment |
add a comment |
number = request.POST['value']
This will return a string and strings don't have a save method so it will not work
I think that you have a model that is referenced by that number and to save it to the database you can do something like this
object= yourmodel.objects.get(pk=number)
object.save()
Also strings don't have a definition for is_valid(), which is used in forms, which are not implemented in your view
After saving to the database you have to redirect, which is done only if nothing is submitted
Final code :
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
# check if number is valid (not empty and no spaces before or after)
if not number.strip():
object = yourmodel.objects.get(pk=number)
object.save()
return render(request, 'home.html', {})
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
1
Hi Amine, I just want to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without objects and models?
– kubaSpolsky
Nov 27 '18 at 8:27
Please see my other answer
– Amine Messaoudi
Nov 27 '18 at 9:46
add a comment |
number = request.POST['value']
This will return a string and strings don't have a save method so it will not work
I think that you have a model that is referenced by that number and to save it to the database you can do something like this
object= yourmodel.objects.get(pk=number)
object.save()
Also strings don't have a definition for is_valid(), which is used in forms, which are not implemented in your view
After saving to the database you have to redirect, which is done only if nothing is submitted
Final code :
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
# check if number is valid (not empty and no spaces before or after)
if not number.strip():
object = yourmodel.objects.get(pk=number)
object.save()
return render(request, 'home.html', {})
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
1
Hi Amine, I just want to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without objects and models?
– kubaSpolsky
Nov 27 '18 at 8:27
Please see my other answer
– Amine Messaoudi
Nov 27 '18 at 9:46
add a comment |
number = request.POST['value']
This will return a string and strings don't have a save method so it will not work
I think that you have a model that is referenced by that number and to save it to the database you can do something like this
object= yourmodel.objects.get(pk=number)
object.save()
Also strings don't have a definition for is_valid(), which is used in forms, which are not implemented in your view
After saving to the database you have to redirect, which is done only if nothing is submitted
Final code :
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
# check if number is valid (not empty and no spaces before or after)
if not number.strip():
object = yourmodel.objects.get(pk=number)
object.save()
return render(request, 'home.html', {})
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
number = request.POST['value']
This will return a string and strings don't have a save method so it will not work
I think that you have a model that is referenced by that number and to save it to the database you can do something like this
object= yourmodel.objects.get(pk=number)
object.save()
Also strings don't have a definition for is_valid(), which is used in forms, which are not implemented in your view
After saving to the database you have to redirect, which is done only if nothing is submitted
Final code :
def shifts_table(request):
if request.method == 'POST':
number = request.POST['value']
# check if number is valid (not empty and no spaces before or after)
if not number.strip():
object = yourmodel.objects.get(pk=number)
object.save()
return render(request, 'home.html', {})
else:
messages.success(request, ('Seems Like There Was An Error...'))
return render(request, 'home.html', {})
else:
return render(request, 'shifts_table.html', {})
edited Nov 26 '18 at 22:57
answered Nov 26 '18 at 22:45
Amine MessaoudiAmine Messaoudi
526514
526514
1
Hi Amine, I just want to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without objects and models?
– kubaSpolsky
Nov 27 '18 at 8:27
Please see my other answer
– Amine Messaoudi
Nov 27 '18 at 9:46
add a comment |
1
Hi Amine, I just want to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without objects and models?
– kubaSpolsky
Nov 27 '18 at 8:27
Please see my other answer
– Amine Messaoudi
Nov 27 '18 at 9:46
1
1
Hi Amine, I just want to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without objects and models?
– kubaSpolsky
Nov 27 '18 at 8:27
Hi Amine, I just want to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without objects and models?
– kubaSpolsky
Nov 27 '18 at 8:27
Please see my other answer
– Amine Messaoudi
Nov 27 '18 at 9:46
Please see my other answer
– Amine Messaoudi
Nov 27 '18 at 9:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490168%2fpost-and-save-django-variable-doesn%25c2%25b4t-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
but if I code like below it doesn't - what does happen when you run that code?
– Alasdair
Nov 26 '18 at 22:42
ERR_CONNECTION_REFUSED .... and like I commented to our colleague Amine, I would like to submit any number, post it and save as a temporal variable 'number' to do some simple calculations in other.py (in a future). Is it possible to post an integer and save it without creating objects and models?
– kubaSpolsky
Nov 27 '18 at 8:29
You can’t set a variable in one request and access it in another. You could save data in the session, or a cookie, or a cache, then refetch it in another view.
– Alasdair
Nov 27 '18 at 8:51