save form data from pure html to datastore - django

I have a simple form
<form name="input" action="" method="get">
Username: <input type="text" name="user" id = "xxx">
<input type="submit" value="Submit">
</form>
On view.py I want to write a function to get value that user input
def add(request):
if request.method =="POST":
abc=request.args.get('xxx')
return render_to_response('myapp/addtour.html')
But it does not work. Please tell me how to do it in Google App Engine or Django

1: You're checking for POST while your form uses GET -- the if block will never fire.
2: xxx is an HTML ID - it has nothing to do with GET/POST. The name parameter does. So request.GET.get('user') - not 'xxx'

Related

How to limit form submit request in Django app

I want to limit submitting request for visitors(anonymous) in my django app.
Suppose 10 or 50 limits per day/month. If they try to search more than my given limit I want to show a message "you have reached your daily or monthly limit!"
How can I do this?
Here is views:
def homeview(request):
if request.method == "POST" and 'text1' in request.POST:
text1 = request.POST.get('text1')
text2 = request.POST.get('text2')
data = my_custom_function(text1, text2)
context = {'data': data}
else:
context = {}
return render(request, 'home.html', context)
here is form in template:
<form action="" method="POST">
{% csrf_token %}
<input class="form-control m-3 w-50 mx-auto" type="text" name="text1" id="text1" placeholder="">
<input class="form-control m-3 w-50 mx-auto" type="text" name="text2" id="text2" placeholder="">
<input class="btn btn-primary btn-lg my-3" type="submit" value="Submit">
</form>
You can use throttling from the Django REST framework. But there is no throttle rate for a month out of the box. Maybe you can use https://stackoverflow.com/a/50371440/4151233 to create a specific class.
Otherwise, I recommend writing your own implementation. This should be adapted to the requirements with questions such as:
Are my users authenticated when making the requests?
How fast should it be and how much load is expected?
Is a standard DB query acceptable or does it need to be implemented
with the cache framework?

How to retrieve data from a form in Django

I am working on a weather app where the client will be entering the name of the city in a search bar, and the name will be used to gain weather data from an API. To get the name of the city from the form, I have created this condition in view:
if request.method == 'POST':
city = request.data
print(city)
however when being printed, this error occurs: 'WSGIRequest' object has no attribute 'data'
What am I doing wrong? Or is there a completely different way to do this?
you must get data from template like this: (name of function in views.py is something same as in url.py.
def something(request):
if request.method == 'POST':
city = request.POST.get('data')
print(city)
data is the name of your input tag in template:
<input name='data'>
Update:
you need to have a action for your form. it includes a url. this url connect you to your view function for this form.
also you must add name to your input tag not your form tag.
template:
<form method="POST" action = "{% url "url_something" %}" >{% csrf_token %}
<div class="field has-addons">
<div class="control is-expanded">
<input class="input" type="text" placeholder="City Name" name="data" >
</div>
<div class="control">
<button class="button is-info" type="submit">
Search City
</button>
</div>
</div>
</form>
in url.py you must have a url like this.:
url(r'^something', views.something, name='url_something'),

access form data in view

I hava a form in my template which is for search.I did not made any form class to it. Is it possible to have access form data in view or should I make a form class to it.
<form class="navbar-form" role="search" action="{% url 'my_url_name' %}" method="get">
<div class="input-group add-on">
<input class="form-control" placeholder="search" name="srch-term" id="srch-term" type="text">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>
I use this form for its style and I can not make this style with Form class
You can access form data in the request.GET (or request.POST if it is a post request) dictionary in your view. For example:
srch_term = request.GET.get('srch-term')
You can by using the QueryDict object you get with the request :
def myview(request):
if request.method == "POST":
data = request.POST
print(data['foo']) # <--Will print the value I entered into my form.
[...]
But be careful with this : There is no validation tool to ensure the data you receive is correctly formated.

Keep the id of an object after a redirection. Django

In order to pass a function with arguments inside one of my template, I used a hidden input that redirects to a view where the function is (the function is used to delete a user the user is "following"). This view then redirects to the previous page thanks to a HttpResponseRedirect(reverse('myview', args=[a number])).
The thing is that the page I redirect to needs an argument (a number). And I would like the user to be redirected to the exact page where he was.
Here is my template:
<form method="post" action="{% url projet.views.remove_relationship %}">
{% csrf_token %}
<input type="hidden" name="remove_relationship" value="{{ user.id }}">
<input type="submit" value="delete" />
And the view:
def remove_relationship(request):
user = request.user
if request.method == 'POST':
user_id = request.POST['remove_relationship']
user_id= int(user_id)
user_name = User.objects.get(id=user_id)
user.userprofile.remove_relationship(person=user_name.userprofile, status=1)
return HttpResponseRedirect(reverse('myoriginalview', args=[A NUMBER]))
The page where the user comes from is like: www.mysite/myoriginalview/ANUMBER. I want to redirect him to this exact page
In the original view, the number corresponds to an object id. (match.id).
So what I wan't to do is getting the match.id of my original view, pass it in my view remove_relationship in order to use it in "args=[match.id]".
So how do I get this ID from my original view and pass it into my new view?
Here is what I tried:
in my models.py:
#models.permalink
def get_absolute_url_remove_relationship(self):
return ('remove_relationship', (), {'dub_id': self.id})
use it in my template:
<form method="post" action="{{get_absolute_url_remove_relationship}}">
{% csrf_token %}
<input type="hidden" name="remove_relationship" value="{{ user.id }}">
<input type="submit" value="delete" />
In my view:
def remove_relationship(request, match_id=None):
match_id= int(match_id)
user = request.user
if request.method == 'POST':
user_id = request.POST['remove_relationship']
user_id= int(user_id)
user_name = User.objects.get(id=user_id)
user.userprofile.remove_relationship(person=user_name.userprofile, status=1)
return HttpResponseRedirect(reverse('terrain', args=[match_id]))
And I do the necessary modifications in my url.py
So when the user clicks on the button "delete" to delete a user he is following, the redirection works well, but the user is not deleted. It looks like the function is not used anymore, while when I used {%url projet.views.remove_relationship %}, the user was deleted, but there was no redirection.
Any help would be very welcome.
PS: The question comes after an other question I asked before (http://stackoverflow.com/questions/13233794/function-with-arguments-in-a-template-django). I opened a new topic in order to be clearer.
You need to pass the args as a tuple while invoking reverse. Also make sure that you use the fully qualified name of the view inclusive of the the app name.
return HttpResponseRedirect(reverse('appname.views.viewname', args=(match_id,)))
Reference

Django enctype="multipart/form-data" not setting POST data

I need to send both a file and some data from input texts.
This is the form I'm working on:
<form method="post" action="{% url catalog_create_ajax_upload %}" enctype="multipart/form-data" id="create-form">
<input type="text" id="new-catalog-name" name="catalog_name" class="large-input" placeholder="catalog title" />
<div id="new-catalog">
<input type="file" name="file">
</div>
</form>
When sent, I excpect request.POST['catalog_name']to have a value, but the whole POST attribute in an empty dictionary.
Any help?
You don't seem to have a submit button in that form. Presumably you've got one elsewhere on the page, but it would only submit the fields in its own form - move it inside that <form>...</form>.
Make sure your view function should post image file like this
def index(request):
image = 'file' in request.FILES and request.FILES['file']
Use request.FILES instead of request.POST