I have a little form:
<form action="#" method="post">
{% csrf_token %}
<label>Company Number:</label>
<input type="text" name="company" placeholder=""/><br>
<input type="submit" id="register value=" OK" />
</form>
Which is mapped like this:
url(r'^userfilt/insertForm/$', views.insertForm, name='insertForm'),
Now after submitting this form, I want to get back to the main view:
url(r'^userfilt/$', views.userfilt, name='userfilt')
URL mapping file:
app_name = 'SSO_Management_POC'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^user/$', views.user, name='user'),
url(r'^userfilt/$', views.userfilt, name='userfilt'),
url(r'^userfilt/insertForm/$', views.insertForm, name='insertForm'),
#url(r'^updateForm/$', views.updateForm, name='updateForm'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
The main view is like this (but I don't think this code is related to the problem..):
def userfilt(request):
if request.GET.get('create'):
return HttpResponseRedirect('insertForm')
if request.GET.get('update'):
print request.GET.get('pk')
return render(request, 'SSO_Management_POC/updateForm.html')
if request.method == 'POST':
form = UserForm(request.POST)
val = request.POST.get('my_textarea')
return render(request, 'SSO_Management_POC/userfilt.html',
{'top_user': TopUser.objects.filter(user_name__regex=val)})
else:
print '4'
return render(request, 'SSO_Management_POC/userfilt.html')
Now the call that is killing me happens when N submit the form, N just wanna get back to the main page calling it with a POST, like a always did!
return render(request, "SSO_Management_POC/userfilt.html")
I do it like this, but the problem is that the URL has not been reset.. and results in this,
http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/#
Resulting in every operation I make on that page not work because it's not mapped anymore
I mean, it should be:
http://127.0.0.1:8000/SSO_Management_POC/userfilt/
but instead it is
http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/#)
To try to explain my issue better..
I go to the main page (http://127.0.0.1:8000/SSO_Management_POC/userfilt/)
"GET /SSO_Management_POC/userfilt/ HTTP/1.1" 200 2648
Then I click on the create User to call the form, it brings me to: (http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/)
"GET /SSO_Management_POC/userfilt/insertForm/ HTTP/1.1" 200 1549
Than I submit the form and I would like to get back to the main page, and here comes the thing I don't understand:
it bring me here
http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/#
But I want to go here
http://127.0.0.1:8000/SSO_Management_POC/userfilt
This is again the code:
return render(request, "SSO_Management_POC/userfilt.html")
This is the call made:
"POST /SSO_Management_POC/userfilt/insertForm/ HTTP/1.1" 200 2648
I tried with render, with HttpResponseRedirect and so on... But always it happened the URL I'm calling with the previous one, I want it to be reset, I want it to be ../ !!!
The only thing that work for me is:
return redirect("../")
but
this is dirty
this does not permit to make a POST call!
Thanks to Daniel, I fixed it like this:
return redirect('/SSO_Management_POC/userfilt')
But it still give me same issue with:
return render(request,'/SSO_Management_POC/userfilt')
Getting me to http://127.0.0.1:8000/SSO_Management_POC/userfilt/insertForm/
You should always redirect, not render, after a successful post. Doing return redirect(url) is the correct thing to do, and it's not clear why you're not happy with this.
Note that redirect can accept the name of a URL pattern, so you could do return redirect('userfilt') which will take you to the correct place.
Does the following code solve your problem?
In your template:
<form action="{% url 'userfilt' %}" method="post">
Related
My intention is to change from interface view -> switch view to process some data and send those data and change to -> test view to display the result. However, nothing in switch view seems to be processed and switch view doesn't change to test view after I hit 'submit' on userInterface.html. My guess is that the problem lies on the HttpResponseRedirect() function or anything related to url paths. Everything worked find with my other project that I worked on my computer. I'm not sure what I need to change to use Django on RaspberryPi.
At first, I found out I didn't import libraries needed for those function. After I imported them, the code was still not working.
I commented out other codes in switch view that do nothing with changing views and just focus on changing view in my switch view.
view.py
def user_interface(request):
return render(request,'zuumcoin/userInterface.html',
{})
def switch(request):
return HttpResponseRedirect(reverse('zuumcoin:test'))
def test(request):
return render(request,'zuumcoin/test.html',{})
userInterface.html
....
<form action="{% url 'zuumcoin:swicht' %} method = "POST">
{% csrf_token %}
...
...
</form>
urls.py
app_name='zuumcoin'
urlpatterns = [
url(r'', views.user_interface, name='interface'),
url(r'switch/', views.switch, name='switch'),
url(r'test/', views.test, name='test')
]
I expect HttpResponseRedirect to direct me to test view instead of being stuck in switch view. If it can do that, I think I can find a way for other part of my code in my switch view to run.
You didn't terminate your regexes. So the first pattern matches every path.
You should do:
url(r'^$', views.user_interface...)
It seems you have typo in your userInterface.html template. change this:
{% url 'zuumcoin:swicht' %}
to this one:
{% url 'zuumcoin:switch' %}
I'm new to Django!
I'm doing a simple registration form, when it is submitted, it returns a label to the previous page, that is the main page:
The form is submitted like this:
<form method="post"">
And its mapped in urls.py:
url(r'^userfilt/insertForm/$', views.insertForm, name='insertForm')
All the urls.py file:
app_name = 'SSO_Management_POC'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^user/$', views.user, name='user'),
url(r'^userfilt/$', views.userfilt, name='userfilt'),
url(r'^userfilt/insertForm/$', views.insertForm, name='insertForm'),
#url(r'^updateForm/$', views.updateForm, name='updateForm'),
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(), name='results'),
url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]
So the related "def" its called:
def insertForm(request):
if request.method == 'POST':
#some stuff here
#sending the get parameter to the main page:
return redirect('/SSO_Management_POC/userfilt/?label=User Registered!')
now when i redirect to the main page i'll see something like:
Now i want just continue working so, I'll put a filter into the input and i perform a search, below the code:
if request.method == 'POST':
form = UserForm(data=request.POST)
val = request.POST.get('my_textarea')
return render(request, 'SSO_Management_POC/userfilt.html', {'top_user': TopUser.objects.filter(user_name__regex=val)})
As you can see is a POST call, but it comes the issue, GET parameter is still there so this cause
And obviously i don't want anymore the label there, it should disappear after another call...
And the url still looks like this:
http://127.0.0.1:8000/SSO_Management_POC/userfilt/?label=User%20Registered!
Now, i know i can resolve this with workaround front end side, but i would like to know:
Is there something that is not good as flow of operations?
How can i resolve this? Where am i wrong..?
I tried to look for something to clean the get parameter in the url, or to reset it, beacuse i thought it was the easier way, but the only things i found costs a lot of code, have you other idea about to clean the url?
is there any other Django method that helps you resolve this, or maybe simply avoid this problem?
The problem is that you don't have an action="" in your form tag which simply means that you want to post to the existing URL, including any querystring (ie. existing GET parameters).
Just add action="" to the form tag, such as:
<form method="POST" action="{% url 'insertForm' %}">
I know this question was asked before, but none worked for me. I have this code that I want it to be executed when a button is clicked and a message is passed
import time
from sinchsms import SinchSMS
number = '+yourmobilenumber'
message = 'I love SMS!'
client = SinchSMS(your_app_key, your_app_secret)
print("Sending '%s' to %s" % (message, number))
response = client.send_message(number, message)
message_id = response['messageId']
response = client.check_status(message_id)
while response['status'] != 'Successful':
print(response['status'])
time.sleep(1)
response = client.check_status(message_id)
print(response['status'])
Basically, what I need is to add an input in a template "HTML File", this input get passed to the message variable in the code above, same with the number. I can easily do that with instances, but how can the below get executed when a button is clicked from the form in the template?
I'm kinda newbie in Django and still finding my way
Here is the tutorial that explains how to make the python file, but execute it from the shell, not a django application.
I hope I was clear describing my problem and any help would be appreciated!
All you need is a form with a message field. In a view, you want to show that form and when the user press submit, you want to execute your script.
Here is some pseudo-code:
urls.py
url('^my-page/' my_views.my_view, name='my-page'),
forms.py
SmsForm(forms.Form):
message = fields.CharField(...)
my_views.py
def my_view(request):
form = SmsForm(data=request.POST or None)
if request.method == 'POST':
if form.is_valid():
send_sms(form.cleaned_data['message']) # do this last
messages.success(request, "Success")
return HttpResponseRedirect(request.path)
else:
messages.warning(request, "Failure")
return render(request, 'my_template.html', {'form': form})
Check the Django documentation about urls, views, forms and messages and proceed step by step:
get the page to load
get the form to load
get the form submission to work and simply show "Success" or "Failure"
finally, write the send_sms function (you've almost done it)
Lets start from the dust cloud.
What you are asking is mostly about how the web pages work. You need to know how to pass parameters using HTML. There are lots of ways to do it. But with django there is a pattern.
You need a url, and a view to catch any requests. Then you need to create a template and a form inside it. With this form you could create some requests to send data to your view.
To create you need to edit urls.py inside your project add an url:
urls.py
from django.conf.urls import url
from my_app.views import my_view
urlpatterns = [
...
url(r'^my_url$', my_view, name='my_view')
...
]
For more about urls please look at URL dispatcher page at documentation.
Then create your view inside your app which is my_app in my example. Edit my_app/views.py
my_app/views.py
from django.http import HttpResponse
def my_view(request):
return HttpResponse('IT WORKS!')
This way you get a working view which could be accessed with path /my_url. If you run ./manage.py runserver you could access your view from http://localhost:8000/my_url.
To create a form you need to create a template. By default django searches app directories for templates. Create a templates directory in your app, in our case my_app/templates and create an HTML file inside. For example my_app/templates/my_form.html. But i advice to create one more directory inside templates directory. my_app/templates/my_app/my_form.html. This will prevent template conflicts. You can check Templates page at documentation for more.
my_app/templates/my_app/my_form.html
<html>
<body>
<form action="/my_url" method="POST">
{% csrf_token %}
<input type="text" name="number">
<input type="text" name="message">
<input type="submit" value="Run My Code">
</form>
</body>
</html>
This is the one of the ways of creating your form. But I do not recommend it. I will make it prettier. But first lets "Make it work", edit your views.py:
csrf_token is a django builtin template tag, to put CSRF token into your form. By default django requires CSRF tokens at every post
request.
my_app/views.py
from django.http import HttpResponse
from django.shortcuts import render
def my_view(request):
if request.method == 'GET':
return render('my_app/my_form.html')
elif request.method == 'POST':
# get post parameters or None as default value
number = request.POST.get('number', None)
message = request.POST.get('message', None)
# check if parameters are None or not
if number is None or message is None:
return HttpResponse('Number and Message should be passed')
# your code goes here
...
return HttpResponse('Your code result')
Till this point the purpose of this answer was "Making it work". Lets convert it nice and clean. First of all we would create Form. Forms are like models, which helps you create forms as objects. It also handles form validations. Forms are saved inside forms directory generally. Create my_app/forms.py and edit it:
my_app/forms.py
from django import forms
class MyForm(forms.Form):
number = forms.CharField(max_length=15, required=True)
message = forms.CharField(max_length=160, required=True)
Put your form inside your template:
my_app/templates/my_app/my_form.html
<html>
<body>
<form action="{% url 'my_view' %}" method="POST">
{% csrf_token %}
{{ form }}
</form>
</body>
</html>
Besides the form, the action of the HTML form tag is also changed.
url template tag is used to get url form url name specified in urls.py.
Instead of url tag, {{ request.path }} could have been used.
Create a form instance and pass it to the template rendering:
my_app/views.py
from django.http import HttpResponse
from django.shortcuts import render
from .forms import MyForm
def my_view(request):
if request.method == 'GET':
form = MyForm()
return render('my_app/my_form.html', {'form': form})
elif request.method == 'POST':
form = MyForm(request.POST)
# check if for is not valid
if not form.is_valid():
# return same template with the form
# form will show errors on it.
return render('my_app/my_form.html', {'form': form})
# your code goes here
...
return HttpResponse('Your code result')
You can use class based vies to write your view, but it's not necessary. I hope it helps.
You can create a view that takes up query parameters from the url and use it for further implementation. Then you can create a link/button in the html template which can redirect you to that url. For example:
in urls.py:
url(r'^run_a/(?P<msg>\w{0,25})/(?P<num>\w{0,25})/$', yourcode, name='get_msg'),
in template:
submit
in views.py:
def get_msg(request,msg,num):
message=msg
number=num
#rest of the code
Hope this helps :)
How to redirect with variables in django?
Please guide me, thank you.
urls.py:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^computer/$', views.computer, name='computer'),
url(r'^result/$', views.result, name='result'),
)
This is my original views.py :
def computer(request):
result = Computer.objects.order_by('?')[:1]
return render(request, 'many/result.html',{'result':result})
And I found I problem, render will not redirect to moneymany/result.html on the url,
so if the user refresh, it will get another result on the same page.
So I have to use redirect to many/result.html .
What's the usually way to redirect in django and I have to pass variable result?
I try this,but not work :
def result(request):
return render(request, 'many/result.html')
def computer(request):
result = Computer.objects.order_by('?')[:1]
url = reverse(('many:result'), kwargs={ 'result': result })
return HttpResponseRedirect(url)
you need
url(r'^result/(?P<result>[^\/]*)/$', views.result, name='result'),
and
return redirect(reverse('many:result', kwargs={ 'result': result }))
or (without changing url)
return redirect('/result/?p=%s' % result )
if you want to maintain POST data while redirecting, then it means your design isnot good. quoting Lukasz:
If you faced such problem there's slight chance that you had
over-complicated your design. This is a restriction of HTTP that POST
data cannot go with redirects.
How about using redirect.
from django.shortcuts import redirect
def computer(request):
result = Computer.objects.order_by('?')[:1]
return redirect('view-name-you-want', { 'result'=result })
this worked with i just needed to pass url parameters as arguments
return redirect('pagename' ,param1 , param2)
my url looks like :
path('page', views.somefunction, name="pagename")
Note: in my case somefunction accept only POST parameters
To redirect from a view to another view with data, you can use session with request.session['key'] and in this case, you don't need to modify path() in "myapp/urls.py" as shown below. Then, give the conbination of the app name "myapp", colon ":" and the view name "dest_view" which is set in path() in "myapp/urls.py" as shown below:
# "myapp/views.py"
from django.shortcuts import redirect
def redirect_view(request):
# Here
request.session['person'] = {'name': 'John', 'age': 27}
# Here
return redirect("myapp:dest_view")
# "myapp/urls.py"
from django.urls import path
from . import views
app_name = "myapp"
urlpatterns = [ # This is view name
path('dest/', views.destination_view, name="dest_view")
]
Then, this is how you get the data of "post" method:
# "myapp/index.html"
{{ request.session.person.name }} {# John #}
{{ request.session.person.age }} {# 27 #}
Right now I have a pyramid (python) app with a home page that is reached via this route in the init.py file:
config.add_route('home_page', '/')
in my views.py file I have:
#view_config(route_name='home_page', renderer='templates/edit.pt')
def home_page(request):
if 'form.submitted' in request.params:
name= request.params['name']
body = request.params['body']
page=Page(name,body)
DBSession.add(page)
return HTTPFound(Location=request.route_url('view_page',pagename=name))
and in my edit.pt template I have
<form action="${save_url}" method="post">
<textarea name="name" tal:content="page.data" rows="10"
cols="60"/><br/>
<textarea name="body" tal:content="page.name" rows="10"
cols="60"/><br/>
<input type="submit" name=form.submitted value="Save"/>
</form>
So basically the goal is to have the homepage show this edit.pt template which contains a form for submitting two pieces of information, a page name and page body. Upon submitting the form, the return HTTPFound function should redirect to the view_page created which shows the page name page body on a new permanent url.
I am not sure what I should add after the if statement in my home_page view_config. If the form hasn't been submitted I don't want anything to happen, it should just continue to show that edit.pt template. Right now I am getting an error when I try to visit the home page: ValueError: renderer was passed non-dictionary as value.
It looks like you are missing a condition
#view_config(route_name='home_page', renderer='templates/edit.pt')
def home_page(request):
if 'form.submitted' in request.params:
name= request.params['name']
body = request.params['body']
page=Page(name,body)
DBSession.add(page)
return HTTPFound(Location=request.route_url('view_page',pagename=name))
# form was not submitted here, need to return context
# ValueError: renderer was passed non-dictionary as value < beacuase you were
# not returning context for this case before
return {} # template contenxt here