You want to use request.args for your GET parameters in Flask. Here is a quote with an example from the Accessing Request Data section of the Quickstart document.
To access parameters submitted in the URL (?key=value) you can use the args attribute:
searchword = request.args.get('key', '')
Answer from istruble on Stack Overflowpython - AttributeError: 'Request' object has no attribute 'get' - Stack Overflow
python - 'Request' object has no attribute 'get' error while getting the api url - Stack Overflow
Please How Can I fix "AttributeError at /items/ 'WSGIRequest' object has no attribute 'Get'"
python - Request' object has no attribute 'get': Django Views &. Serializers - Stack Overflow
Videos
I have this idea to put a confirm/deny in list_page so that when I click confirm it automatically sends an email to the user that he or she is approved. I have put a confirm/deny in list_page successfully, but it gives me an attribute error when I click confirm. Can someone help me what's wrong with my code?
this is my views
@admin.register(Application)
class ApplicationAdmin(admin.ModelAdmin):
list_display = ['user', 'confirmed', 'denied']
def confirmed(self, obj):
url = reverse('admin:confirm_url', kwargs={'id': obj.id})
return format_html('<a class="button" href="{}">Confirm</a>', url)
def denied(self, obj):
url = reverse('admin:denied_url', kwargs={'id': obj.id})
return format_html('<a class="button" href="{}">Deny</a>', url)
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path('confirm/<int:id>', self.confirmed_application, name='confirm_url'),
path('deny/<int:id>', self.denied_application, name='denied_url'),
]
return custom_urls + urls
def confirmed_application(self, request, id):
# you get object_id you can do whatever you want
# you can send a mail
self.object = self.get_object(request, id)
subject = 'Approved'
message = f'Your application has been approved.'
email_from = settings.EMAIL_HOST_USER
recipient_list = [self.request.user.email]
send_mail(subject, message, email_from, recipient_list)
# after processed all logic you can redirect same modeladmin changelist page
redirect_url = "admin:{}_{}_changelist".format(self.opts.app_label, self.opts.model_name)
return redirect(reverse(redirect_url))
def denied_application(self, request, id):
# same as confirmed_application
self.object = self.get_object(request, id)
subject = 'Rejected'
message = f'Your application has been denied.'
email_from = settings.EMAIL_HOST_USER
recipient_list = [self.request.user.email]
send_mail(subject, message, email_from, recipient_list)
redirect_url = "admin:{}_{}_changelist".format(self.opts.app_label, self.opts.model_name)
return redirect(reverse(redirect_url))The urllib module has been split into parts and renamed in Python 3 to urllib.request, urllib.parse, and urllib.error.
Import urllib.request instead of urllib.
import urllib.request
Interestingly, I noticed some IDE-depending behavior.
Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do
import urllib.request
while in Spyder,
import urllib
does fine
This is the typical symptom of an unrelated requests.py (or requests.pyc) file sitting in your current directory, or somewhere else on the PYTHONPATH. If this is the case, remove or rename it, as it's shadowing the module you really want to import.
You are importing all names from the requests module into your local namespace, which means you do not need to prefix them anymore with the module name:
>>> from requests import *
>>> get
<function get at 0x107820b18>
If you were to import the module with an import requests statement instead, you added the module itself to your namespace and you do have to use the full name:
>>> import requests
>>> requests.get
<function get at 0x102e46b18>
Note that the above examples is what I got from my tests in the interpreter. If you get different results, you are importing the wrong module; check if you have an extra requests.py file in your python package:
>>> import requests
>>> print requests.__file__
/private/tmp/requeststest/lib/python2.7/site-packages/requests/__init__.pyc
You can also test for the name listing provided by the requests module:
>>> print dir(requests)
['ConnectionError', 'HTTPError', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', '_oauth', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'defaults', 'delete', 'exceptions', 'get', 'head', 'hooks', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'safe_mode', 'session', 'sessions', 'status_codes', 'structures', 'utils']
I'm very new to Python, and am practicing using the Requests library. However, whenever I run this:
import requests
r=requests.get("https://automatetheboringstuff.com/files/rj.txt")
print(len(r.text))I am given the message:
AttributeError: 'module' object has no attribute 'get'
I'm using Python 3 in Canopy.
I've spent a while googling, but none of the solutions that others have suggested worked. I can't find any other .py file on my computer that is named 'Requests', for instance. When I run:
print(dir(requests))
I get:
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'binary', 'do_not']
Can anyone help? Thanks in advance!
Your problem is here:
intention = Intention.objects.get(pk=id)
form = IntentionForm(intention) # An unbound form
The first argument to a form is the data but you are passing the instance. To properly pass the instance you should use:
intention = Intention.objects.get(pk=id)
form = IntentionForm(instance=intention) # An unbound form
The above answer is correct, however, this error can also be generated by incorrectly passing arguments in the init of a form, which is used for an admin model.
Example:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(self, *args, **kwargs)
Notice the double passing of self? It should be:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
Hi. I have this problem "AttributeError at /Post/ 'Post' object has no attribute 'get'"
and have no clue why it happens. I am using a django filter and it's my first time trying "foreignkey"
There are two simple models and Post_price is the one that has a foreignkey attribute.
Thank you for your help in advance.
<These are the models.py files>
class Post(models.Model):
name= models.CharField(max_length=11,default='')
class Post_price(models.Model):
post= models.ForeignKey('Post', blank=True, null=True, on_delete=models.SET_NULL)
content= models.IntegerField(default='')
<it's a django filter file>
class PostFilter(django_filters.FilterSet):
class Meta:
model = Post
fields = {
'name': ...(I just handled it well)
<This one is for views.py>
def Post(request):
s = Post.objects.all()
aq = PostFilter(request.GET, queryset=s)
return render(request,'app/Post.html',{'s':aq})
<just in case urls.py's path part>
path('Post/',Post,name='Post'),
Your function definition def Post(request): overwrites from .models import Post. You need to be more careful with how you write your variables & function/class definitions.
You might want to rename your variables/functions/classes so they’re easier to read and understand, and to avoid collisions.