I'm getting this error in this function in my views.py file. It's confusing because I don't know what 'WSGIRequest' is or why it's giving me problems. I know I have a variable called "newUser" because when I take out that one line the print(request.POST) line prints it out.
def AddNewUser(request):
a=AMI()
if(request.method == "POST"):
print(request.POST)
print(request["newUser"])
csrfContext = RequestContext(request)
return render_to_response("ac/AddNewUser.html", csrfContext)
`
Why am I getting this error?
It means that WSGIRequest does not implement __getitem__. You are trying to treat the HttpRequest object like a dictionary but it's not. If you want to access this newUser variable use the POST object, which implements a dictionary-like interface:
request.POST['newUser']
You'd do well to peruse the Django docs in situations like this.
Related
I want to assign the input of a form to a function in my view but I keep getting this error. Please help do I fix it.
Error
receiver = list(ToolsForm.declared_fields['receiver_mail'])
TypeError: 'CharField' object is not iterable
You can obtain the value associated with the receiver_mail field with:
receiver = form.cleaned_data['receiver_mail']
so without using list(…) part and with .cleaned_data [Django-doc], the form should be an instance of ToolForm, not a reference to the ToolForm class. Before you can retrieve the data, you will first need to validate the form, so:
form = ToolsForm(request.POST, request.FILES)
if form.is_valid():
receiver = form.cleaned_data['receiver_mail']
I have a media model and a product model. When a user creates a product they first upload a picture and then after this, they're forwarded to the product detail page where they can edit the products attributes. This works fine, however if the user doesn't upload a picture I'd like the program to skip creating a media object and just go straight to the product detail page.
I've tried returning a reverse() function from form_valid() but this doesn't work so I'm wondering if anyone knows why this is and how I can fix this?
My code currently:
class ProductMediaCreate(generic.CreateView):
model = ProductMedia
fields = ('media',)
template_name = 'media/media_create.html'
def form_valid(self, form):
product_obj = Product.objects.create()
if not form.instance.media:
return reverse('product_detail', kwargs={'pk': product_obj.pk})
form.instance.product = product_obj
return super().form_valid(form)
def get_success_url(self):
return reverse('product_detail', kwargs={'pk': self.product.pk})
However this produces the error: 'str' object has no attribute 'get' when I submit the form on the CreateView without a picture.
EDIT: not sure if this'll help, but following the traceback for the error states the source of the error to be within clickjacking.py on the line: if response.get('X-Frame-Options') is not None: (I'm not sure what this means though)
Thanks in advance for any help!
- GoingRoundInCircles
I figured it out!
You can't directly use reverse() in form_valid as form_valid() is expecting a response so you have to use HttpResponseRedirect() around the reverse function.
i.e.
if not form.instance.media:
return HttpResponseRedirect(reverse('product_detail', kwargs={'pk': product_obj.pk}))
To get HttpResponseRedirect you import it from django.http:
from django.http import HttpResponseRedirect
Hope that helps someone in the future! :)
When I submit a certain CreateView, I want to move on to create another object in another CreateView. However, when I try
def get_success_url(self):
return FooView.as_view()(self.request, itempk = self.object.id)
I get
ContentNotRenderedError: The response content must be rendered before
it can be accessed.
I also tried
def get_success_url(self):
return render(FooView.as_view()(self.request, itempk = self.object.id))
which gives me
AttributeError: 'TemplateResponse' object has no attribute 'META'
I'm fairly certain I'm just going about this the wrong way, and that I've done it correctly before, but I'm stumped. What is the proper way to do this?
You don't want to call the view, you want to redirect the user to it. So just use the redirect function:
from django.shortcuts import redirect
...
return redirect('foo_view_name', kwargs={'itempk': self.object.id})
Since you're defining the get_success_url, I would say that you just need something like
def get_success_url(self):
# assuming that your FooView urlconf was named "foo_view"
return reverse('foo_view', kwargs={'itempk':self.object.id})
Cf. https://docs.djangoproject.com/en/dev/ref/urlresolvers/
Am working on a web2py HTML view but keep getting an erros.
This is the code:
{{extend 'layout.html'}}
<h2>Edit your post</h2>
<h3>for category {{=form.record.category.name.title()}}</h3>
{{=form}}
and the error:
AttributeError: 'NoneType' object has no attribute 'name'
How can i fix the error?
N/B controller:
def edit_post():
id = post.id
form = SQLFORM(= A("Edit post",_href=URL(request.args=auth.user.id/login))
return locals()
Please see the SQLFORM documentation about how you create the form. I assume you've changed the code before you posted it up here, since python wouldn't compile it because of the = in the parameter list of SQLFORM.
I want to send a mail to every user that fill a form on my django website. After writing a code for it, I'm getting the below error. I've been trying to find the cause of the error but no success. Below are my codes:
AttributeError at /meek/
'NoneType' object has no attribute 'e_mail'
Views
Subject='Welcome'
message=loader.get_template('letter.txt')
from_email='men#men.com'
def gent_me(request):
if request.method=='POST':
form=GentForm(request.POST)
if form.is_valid():
data=form.cleaned_data
newgent=Gent(
pub_date=datetime.datetime.now(),
full_name=data['full_name'],
company_name=data['company_name'],
services=data['services'],
e_mail=data['e_mail'],
address=data['address'],
city=data['city'],
state=data['state'],
phone_no=data['phone_no'])
invite=newgent.save()
send_mail(Subject, message.render(Context()),from_email,[invite.e_mail])
return HttpResponse('Thanks. Kindly check your mail.')
else:
return HttpResponse('Kindly fill form.')
else:
return render_to_response('ment.html',{'GentForm':GentForm},context_instance=RequestContext(request))
How can I fix this?
The Model.save method has a return value of 'None'. So 'invite' after invite=newgent.save() is None. Instead of
invite=newgent.save()
you should do
newgent.save()
and then
send_mail(Subject, message.render(Context()),from_email,[newgent.e_mail])