I am trying to create a form(boqform) to post data into a model(boqmodel)
but I am getting this
typeerror is_valid() missing 1 required positional argument: 'self'
I need to display a form called boqform on html template and post the data user entered into boqmodel
my view:
def boqmodel1(request):
if boqform.is_valid():
form = boqform(request.POST)
obj=form.save(commit=False)
obj.save()
context = {'form': form}
return render(request, 'create.html', context)
else:
context = {'error': 'The post has been successfully created. Please enter boq'}
return render(request, 'create.html', context)
Traceback:
File "C:\Users\TRICON\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\TRICON\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\TRICON\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\TRICON\Desktop\ind\ind\boq\views.py" in boqmodel1
23. if boqform.is_valid():
Exception Type: TypeError at /boq/create/
Exception Value: is_valid() missing 1 required positional argument: 'self'
You're trying to call the instance function is_valid() on the class boqform. You need to get an instance of boqform first. Switching lines 1 and 2 of the function should fix the issue:
def boqmodel1(request):
form = boqform(request.POST)
if form.is_valid():
obj=form.save(commit=False)
obj.save()
context = {'form': form}
return render(request, 'create.html', context)
else:
context = {'error': 'The post has been successfully created. Please enter boq'}
return render(request, 'create.html', context)
Related
I don't seem to find a way to solve an error happening at the user authentication.
User needs to login at the public website level let's say localhost/login and when he/she is authenticated, I need the user to be redirected in the corresponding sub domain, for example demo.localhost. This is not working for me so far, even after checking some posts about the subject.
Here is the views.py in customers.views where user log in:
def login_view(request):
if request.method == 'POST':
form = AuthenticationForm(request,data=request.POST)
print("form login view")
if form.is_valid():
print("checkpoint")
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
user = form.get_user()
schema_name = user.schema_name
print(user)
if user is not None:
login(request, user)
url = reverse(schema_name + '.localhost:8000/mydashboard/')
print("url")
return HttpResponseRedirect(url)
else:
print("not working")
form = AuthenticationForm()
context = {
'form': form,
}
return render(request, 'login.html', context)
here is what urls in the core folder:
urlpatterns = [
path('admin/', admin.site.urls),
path('django_plotly_dash/', include('django_plotly_dash.urls')),
path('mydashboard/', include('mydashboard.urls',namespace="mydashboard")),
]
and I have added app_name = 'mydashboard' in mydashboard.urls
Here is the traceback of the error I keep getting:
Internal Server Error: /registration/loginUser
Traceback (most recent call last):
File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/urls/base.py", line 71, in reverse
extra, resolver = resolver.namespace_dict[ns]
KeyError: 'demo.localhost'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/pierre/Desktop/simulation_application/customers/views.py", line 65, in login_view
url = reverse(schema_name + '.localhost:8000/mydashboard/')
File "/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/urls/base.py", line 82, in reverse
raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'demo.localhost' is not a registered namespace
UPDATE:
Remove the reverse and included request.scheme in the url.
def login_view(request):
if request.method == 'POST':
form = AuthenticationForm(request,data=request.POST)
print("form login view")
if form.is_valid():
print("checkpoint")
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
user = form.get_user()
schema_name = user.schema_name
print(user)
if user is not None:
login(request, user)
print(request.scheme)
url = request.scheme + '://' + schema_name + '.localhost:8000/mydashboard/'
print("url",url)
return HttpResponseRedirect(url)
else:
print("not working")
form = AuthenticationForm()
context = {
'form': form,
}
return render(request, 'login.html', context)
this is still working and output the following error:
response = get_response(request) …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/core/handlers/base.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/contrib/auth/decorators.py, line 32, in _wrapped_view
return redirect_to_login( …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/contrib/auth/views.py, line 190, in redirect_to_login
return HttpResponseRedirect(urlunparse(login_url_parts)) …
Local vars
/Users/pierre/opt/anaconda3/lib/python3.9/site-packages/django/http/response.py, line 507, in __init__
raise DisallowedRedirect("Unsafe redirect to URL with protocol '%s'" % parsed.scheme) …
Local vars
Unsafe redirect to URL with protocol 'localhost'
Bad Request: /mydashboard/simulations_list
HTTP GET /mydashboard/simulations_list 400 [0.09, 127.0.0.1:50218]
This is strange because the requested url is perfectly working url, registered and accessible without a problem before trying to redirect to it after the login
EDIT #2:
Found out that the above code works when the target url does not require login required
# this does not work
#login_required
def simulationslistView(request,):
return render(request, 'simulations_list.html')
# this works
def simulationslistView(request,):
return render(request, 'simulations_list.html')
I am obviously make sure that the app is secured and can only be accessed by logged in users. Is there a work around to make sure it works?
reverse is an internal django utility to go from "app name" + ':' + "url friendly name" to "real url". In your example, you're already providing the "real url".
Quit while you're ahead and just do:
if user is not None:
login(request, user)
url = request.scheme + '://' + schema_name + '.localhost:8000/mydashboard/'
print("url")
return HttpResponseRedirect(url)
Notice no reverse
Notice the adding of the URL scheme (http/https determined by how the request was originally made)
my django app has a form view for submitting information that creates a new post, after which it should redirect to a thank you page.
def submit(request, slug):
account = get_object_or_404(Account, slug=slug)
if request.method == "POST":
# process the data and post form as JSON
ret = create_or_edit_post(request, account, post=None, duplicate=False)
if isinstance(ret, HttpResponseRedirect):
post = account.posts.latest('id')
# handle post info and post creation
return redirect(reverse('post-submit-ty', kwargs={'slug': account.slug}))
return ret
context = { "account": account }
return render(request, 'submit.html', context)
since the redirect(reverse(...)) returns a tuple, this throws:
AttributeError: 'tuple' object has no attribute 'get'
File "django/core/handlers/base.py", line 131, in get_response
response = middleware_method(request, response)
File "newrelic/hooks/framework_django.py", line 325, in wrapper
return wrapped(*args, **kwargs)
File "django/middleware/clickjacking.py", line 32, in process_response
if response.get('X-Frame-Options') is not None:
since I need to pass slug kwarg (as a tuple) and redirect to the thank you page but it's looking for an http response is there a way to handle this with redirect(reverse) or HttpResponseRedirect(reverse)? thanks
Actually you can pass a view name with arguments and redirect is gonna do internally a reverse.
Try this, redirect('post-submit-ty',slug=account.slug).
Django Shourtcuts redirect
I am trying to log in a user but I am getting an attribute error.
Here is my forms.py:
class Login(forms.Form):
email = forms.EmailField(max_length=250)
password = forms.CharField(widget=forms.PasswordInput)
def login_user(self):
email = self.cleaned_data['email']
password = self.cleaned_data.get('password')
user = authenticate(email=email, password=password)
if user in User.objects.all():
login(self, user)
else:
return render(self, 'todoapp/waiting_2.html')
Here is my views.py:
def login_user(request):
if request.method == 'POST':
login_form = Login(request.POST)
if login_form.is_valid():
login_form.login_user()
login_form.save()
return HttpResponseRedirect(reverse('dashboard'))
else:
return render(request, 'todoapp/waiting_2.html')
return render(request, 'registration/login.html', {'form': Login()})
When I fill in the fields and try to log in, I am getting the error:
AttributeError at /login/
'Login' object has no attribute 'session'
Traceback:
File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/gblp250/PycharmProjects/assignment/todoapp/views.py" in login_user
48. login_form.login_user(request)
File "/home/gblp250/PycharmProjects/assignment/todoapp/forms.py" in login_user
27. login(self, request, user)
File "/home/gblp250/PycharmProjects/practice/venv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py" in login
126. if SESSION_KEY in request.session:
Exception Type: AttributeError at /login/
Exception Value: 'Login' object has no attribute 'session'
There are a few errors here. The main one of attempting to render within the form login_user method. Apart from anything else, you attempt to pass the self as the request parameter to render, which mages no sense.
Remove all of that if/else. You don't need to render; but also note that your if condition is needlessly inefficient. If you get a user, it's necessarily a User.
Finally, the actual cause of your error, where again you are trying to pass self in the place of a request, but this time as the parameter to login. That code belongs in the view.
And finally, the form is not a ModelForm, so there is no save method.
So, form:
def login_user(self):
email = self.cleaned_data['email']
password = self.cleaned_data.get('password')
return authenticate(email=email, password=password)
and view:
if login_form.is_valid():
user = login_form.login_user()
if user:
login(request, user)
return HttpResponseRedirect(reverse('dashboard'))
Although at this point you may as well move all that logic to the view.
login() get as first argument the request, you call it with the form as first argument.
https://docs.djangoproject.com/en/2.1/topics/auth/default/#django.contrib.auth.login
I get the following error:
Traceback:
File "C:\Users\HP\GST\lib\site-packages\django\core\handlers\exception.py"
in inner
35. response = get_response(request)
File "C:\Users\HP\GST\lib\site-packages\django\core\handlers\base.py" in
_get_response
128. response = self.process_exception_by_middleware(e,
request)
File "C:\Users\HP\GST\lib\site-packages\django\core\handlers\base.py" in
_get_response
126. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "C:\Users\HP\Desktop\erpcloud\accounts\views.py" in change_password
31. if form.is_valid():
File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in is_valid
179. return self.is_bound and not self.errors
File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in errors
174. self.full_clean()
File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in
full_clean
376. self._clean_fields()
File "C:\Users\HP\GST\lib\site-packages\django\forms\forms.py" in
_clean_fields
397. value = getattr(self, 'clean_%s' % name)()
File "C:\Users\HP\GST\lib\site-packages\django\contrib\auth\forms.py" in
clean_old_password
366. if not self.user.check_password(old_password):
File "C:\Users\HP\GST\lib\site-packages\django\contrib\auth\models.py" in
check_password
396. raise NotImplementedError("Django doesn't provide a DB
representation for AnonymousUser.")
Exception Type: NotImplementedError at /accounts/change-password/
Exception Value: Django doesn't provide a DB representation for
AnonymousUser.
My view looks like this:
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, user=request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, user)
return redirect(reverse('company:Dashboard'))
else:
return redirect(reverse('accounts:change_password'))
else:
form = PasswordChangeForm(user=request.user)
args = {'form': form}
return render(request, 'accounts/change_password.html', args)
Firstly, I thought that this was due to the fact that I didn't have Django updated, but now I have, and I receive the same error.
I've looked at some solutions that other users asked, but none applied in my case
Any help, please ?
There is nothing wrong with the view itself. The problem is that if a user is not logged in, then request.user will point to a AnonymousUser object. You can see it as a virtual user. This user however has no database representation, since we do not know anything about the user. It is more used to provide a uniform interface.
Now since request.user is an AnonymousUser, you aim to change the password of that user, but you can not store that into a database, hence the error.
The user thus first needs to log in, then request.user will be a real user, and updating the password should work.
I advise however to add a #login_required decorator to the view to prevent this scenario from happening:
from django.contrib.auth.decorators import login_required
#login_required
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, user=request.user)
if form.is_valid():
form.save()
update_session_auth_hash(request, user)
return redirect(reverse('company:Dashboard'))
else:
return redirect(reverse('accounts:change_password'))
else:
form = PasswordChangeForm(user=request.user)
args = {'form': form}
return render(request, 'accounts/change_password.html', args)
I am trying to make a view that i can use in multiple apps with different redirect urls:
Parent function:
def create_order(request, redirect_url):
data = dict()
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
form.save()
return redirect(redirect_url)
else:
form = OrderForm()
data['form'] = form
return render(request, 'core/order_document.html', data)
Child function:
#login_required()
def admin_order_document(request):
redirect_url = 'administrator:order_waiting_list'
return create_order(request, redirect_url)
When i'm trying to call admin_order_document function i'm getting:
Traceback (most recent call last):
File "/home/project/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/project/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: create_order() missing 1 required positional argument: 'redirect_url'
If i remove redirect_url from both functions and manually add 'administrator:order_waiting_list' to redirect() it works, but i need to redirect to multiple urls. So, why am i getting this error?
url(r'^orders/create/', views.create_order, name='create_order')
This clearly is not going to work, since create_order requires redirect_url but there is no redirect_url kwarg in the regex r'^orders/create/'.
Perhaps you want to use the admin_order_document view here instead:
url(r'^orders/create/', views.admin_order_document, name='create_order')
Note you should add a trailing dollar, i.e. r'^orders/create/$' unless you want to match orders/create/something-else as well as orders/create/.
If you didn't changed the regular url
urlpatterns = [
url(r'^admin/', admin_site.urls),
...
]
of your admin site you need to call your function like that:
#login_required()
def admin_order_document(request):
redirect_url = 'admin:order_waiting_list'
return create_order(request, redirect_url)
That should fix your problem.