local variable 'username' referenced before assignment django - python

I have created a function called a panel.
When i run the server it gives an error " UnboundLocalError at /service-panel/ local variable 'username' referenced before assignment" .
I don't know what is getting wrong.
Here is views.py
#login_required
def panel(request):
if(request.session.has_key('username')):
username = request.session['username']
data = Enquiry.objects.filter(service=request.session['service'])
return render(request, 'service-provider-panel/base.html', {'username':username, 'data':data})

You have a logical error:
#login_required
def panel(request):
if(request.session.has_key('username')):
username = request.session['username']
data = Enquiry.objects.filter(service=request.session['service'])
else:
# else statement is needed because the variables username and data must be defined.
username = None
data = None
return render(request, 'service-provider-panel/base.html', {'username':username, 'data':data})

The problem is that the condition request.session.has_key('username') can be False. Then, the username variable will not be assigned. The same with data variable.
A solution could be:
#login_required
def panel(request):
username = None
data = None
if(request.session.has_key('username')):
username = request.session['username']
data = Enquiry.objects.filter(service=request.session['service'])
return render(request, 'service-provider-panel/base.html', {'username':username, 'data':data})
You will need to control the logic in the template (service-provider-panel/base.html) when data or username is None.

Related

global variable not defined in django

I have a problem with this code in django
I have defined two global variables
But Django does not identify them
my view:
global phone,rand_num
def phone_login(request):
if request.method == 'POST':
form = PhoneLoginForm(request.POST)
if form.is_valid():
phone = f"0{form.cleaned_data['phone']}"
rand_num = randint(1000, 9999)
api = KavenegarAPI('mytoken!')
params = { 'sender' : '', 'receptor': phone , 'message' : rand_num }
api.sms_send(params)
return redirect('account:verify')
else :
form = PhoneLoginForm()
return render(request,'account/phone_login.html',{'form':form})
def verify(request):
if request.method == "POST":
form = VerifyCodeForm(request.POST)
if form.is_valid():
if rand_num == form.cleaned_data['code']:
profile = get_object_or_404(Profile, phone = phone)
user = get_object_or_404(User,profile__id = profile.id)
login(request,user)
messages.success(request,'logged in successfully' , 'success')
return redirect('popasssts:all_posts')
else:
messages.error(request,'your code is wrong','warning')
else:
form = VerifyCodeForm()
return render(request,'account/verify.html',{'form' : form})
my urls :
path('verify/',views.verify,name='verify'),
i have this error :
NameError at /account/verify/
name 'rand_num' is not defined
Request Method: POST
Request URL: http://127.0.0.1:8000/account/verify/
Django Version: 3.0.7
Exception Type: NameError
Exception Value:
name 'rand_num' is not defined
I want the user to enter the site after entering the SMS code.
Regarding the global variables, you need to put the global keyword inside the functions and assign initial values to the variables outside:
phone = ""
rand_num = -1
def phone_login(request):
global phone, rand_num
# ...
def verify(request):
global phone, rand_num
# ...
With this approach, the values of phone and rand_num are shared among all users of the application. If your application have multiple users, a better approach would be to store the values in the current user's session:
def phone_login(request):
if request.method == 'POST':
# ...
if form.is_valid():
# ...
# Save the values in the session
request.session["phone"] = phone
request.session["rand_num"] = rand_num
# ...
def verify(request):
if request.method == "POST":
# ...
if form.is_valid():
# Get the values from the session, setting
# default values in case they don't exist.
phone = request.session.get("phone", "")
rand_num = request.session.get("rand_num", -1)
# ...
For using sessions, the django.contrib.sessions application must be enabled in the INSTALLED_APPS list of the settings.py file of the Django project. Also, this application must be migrated to the project's database with the command python manage.py migrate.
In the official Django documentation you have more information about sessions.
Caution: Global variables violates maybe the most important principle of the programming, the encapsulation. Using them, will turn your code to a spaghetti. Don't use them. (Unless there is another way)
Here is what encapsulation means:
...Encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components.
Source: Wikipedia
If you really want to use it, here is your problem: global keyword should be used in functions.
Let's try that way:
phone = ""
rand_num = 0
def phone_login(request):
global phone, rand_num
if request.method == 'POST':
...
def verify(request):
global phone, rand_num
if request.method == "POST":
...

Get Django Auth "User" id upon Form Submission

I currently have a model form that submits an entered domain to the db.
The problem I'm encountering is, I need to save the currently logged in user's ID (PK from the django.auth table) when a domain is submitted to satisfy a PK-FK relationship on the db end.
I currently have:
class SubmitDomain(ModelForm):
domainNm = forms.CharField(initial=u'Enter your domain', label='')
FKtoClient = User.<something>
class Meta:
model = Tld #Create form based off Model for Tld
fields = ['domainNm']
def clean_domainNm(self):
cleanedDomainName = self.cleaned_data.get('domainNm')
if Tld.objects.filter(domainNm=cleanedDomainName).exists():
errorMsg = u"Sorry that domain is not available."
raise ValidationError(errorMsg)
else:
return cleanedDomainName
and views.py
def AccountHome(request):
if request.user.is_anonymous():
return HttpResponseRedirect('/Login/')
form = SubmitDomain(request.POST or None) # A form bound to the POST data
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
domainNmCleaned = form.cleaned_data['domainNm'] ## clean data in dictionary
clientFKId = request.user.id
form.save() #save cleaned data to the db from dictionary`
try:
return HttpResponseRedirect('/Processscan/?domainNm=' + domainNmCleaned)
except:
raise ValidationError(('Invalid request'), code='300') ## [ TODO ]: add a custom error page here.
else:
form = SubmitDomain()
tld_set = request.user.tld_set.all()
return render(request, 'VA/account/accounthome.html', {
'tld_set':tld_set, 'form' : form
})
The problem is it gives me an error of: (1048, "Column 'FKtoClient_id' cannot be null"), very odd thing happening, for the column FKtoClient, its trying to submit: 7L instead of 7(the PK of this user's record). Any ideas?
If someone can please help, I would really appreciate it
Firstly, remove FKtoClient from your form. You need to set the user in your view where you can yes the request object. It's not possible to set an attribute on the form that automatically sets the current user.
When instantiating your form, you can pass a tld instance which already has the user set.
def AccountHome(request):
# I recommend using the login required decorator instead but this is ok
if request.user.is_anonymous():
return HttpResponseRedirect('/Login/')
# create a tld instance for the form, with the user set
tld = Tld(FKtoClient=request.user)
form = SubmitDomain(data=request.POST or None, instance=tld) # A form bound to the POST data, using the tld instance
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
domainNm = form.cleaned_data['domainNm']
form.save() #save cleaned data to the db from dictionary
# don't use a try..except block here, it shouldn't raise an exception
return HttpResponseRedirect('/Processscan/?domainNm=%s' % domainNm)
# No need to create another form here, because you are using the request.POST or None trick
# else:
# form = SubmitDomain()
tld_set = request.user.tld_set.all()
return render(request, 'VA/account/accounthome.html', {
'tld_set':tld_set, 'form' : form
})
This has an advantage over #dm03514's answer, which is that you can access the user within form methods as self.instance.user if required.
If you want to Require that a user be logged in to submit a form, you could do something like:
#login_required # if a user iS REQUIRED to be logged in to save a form
def your_view(request):
form = SubmitDomain(request.POST)
if form.is_valid():
new_submit = form.save(commit=False)
new_submit.your_user_field = request.user
new_submit.save()
You can get the logged in user from the request object:
current_user = request.user

Django -- Redirecting Views with Parameters

In the Django app I am building I would like to have the user creation process go as follows: As user signs up, if valid is then redirected to create a LIST object, and if valid is then redirected to what will be a dashboard for the LIST object just created. My views.py are as follows:
def user_signup(request):
if request.method == 'POST':
form = forms.UserSignupForm(data=request.POST)
if form.is_valid():
user = form.save()
g = Group.objects.get(name='test_group')
g.user_set.add(user)
# log user in
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
login(request, user)
messages.success(request, u'Welcome to Social FollowUp')
return redirect('user_create')
else:
form = forms.UserSignupForm()
return TemplateResponse(request, 'user_signup.html', {
'form': form,
})
#login_required
#permission_required('')
def user_create(request):
if request.method == 'POST':
list_form = forms.ListForm(request.POST)
if list_form.is_valid():
list_create = list_form.save()
messages.success(request, 'List {0} created'.format(list_create.list_id))
return redirect('user_dashboard')
else:
list_form = forms.ListForm()
return TemplateResponse(request, 'dashboard/create.html', {'list_form': list_form, })
def user_dashboard(request, list_id):
try:
list_id = models.List.objects.get(pk=list_id)
except models.List.DoesNotExist:
raise Http404
return TemplateResponse(request, 'dashboard/view.html', {'list_id': list_id})
My urls.py for these views is as follows:
url(r'user/signup/$', views.user_signup, name='user_signup'),
url(r'u/dashboard/(?P<list_id>\d+)/$', views.user_dashboard, name='user_dashboard'),
url(r'u/list/create/$', views.user_create, name='user_create'),
When I try to run through the process, the first two views work correctly. However when I redirect to the user_dashboard I get the following error:
Reverse for 'user_dashboard' with arguments '' and keyword arguments '{}' not found.
which sites this:
return redirect('user_dashboard')
I'm assuming this has something to do with me not passing in a list_id, however, even when I tried to pass in a hardcoded value it did not work (like this):
return redirect('user_dashboard', {'list_id': 2})
What am I doing wrong here?
Try:
return redirect(reverse('user_dashboard', args=(2,)))
Your code
return redirect('user_dashboard')
would not work because in your url pattern, you have
url(r'u/dashboard/(?P<list_id>\d+)/$', views.user_dashboard, name='user_dashboard'),
which requires list_id as a parameter.

Error on django - .save() function

class Member(models.Model):# member db table
userID = models.CharField(max_length=80,primary_key=True) #user id
password = models.CharField(max_length=32)# password
nickname = models.CharField(max_length=100)# user nickname
penalty = models.PositiveSmallIntegerField(max_length=10,default=0,null=True)
participation=models.ForeignKey('Room',default=None,blank=True,null=True)
def __unicode__(self):
return self.userID
def doJoin(request):
if request.is_ajax() and request.method == 'POST':
# check validation
userID = request.POST['userID']
userNickname = request.POST['nickname']
if (checkID(userID) == False) and (checkNickname(userNickname) == False) :
#save to the database
newUser = Member()
newUser.userID = userID
newUser.nickname = userNickname
newUser.password = request.POST['password']
print newUser.userID , newUser.nickname , newUser.password , newUser.penalty , newUser.participation
newUser.save() #<------------error line!!!!
return HttpResponse('true')
else:
return HttpResponse('false')
else:
HttpResponse('false')
line about 8
In function doJoin:
newUser.save() # <--- error... so sad...
What should I do? Help me please.
What's wrong in this source?
Do you have debugging turned off? If you're getting a 500 and you have debugging turned on, you'll get a stack trace with the exception.
What are checkID() and checkNickname() doing? If those are performing some sort of validation, you really should be doing that in a form class instead of in the view. I also wouldn't be pulling values directly out of the request.POST to populate your model. I would highly recommend retrieving those values from a form's cleaned_data dictionary.

Commit a variable to the next function

I just simple want to pass the emailadress from def send_username to the second def username_is_send. How do I do it? How can I pass the variable to the next def?
#csrf_protect
def send_username(request, template_name='auth/user/registration/send_username_form.html',
email_template_name='auth/user/registration/send_username_email.html',
send_username_form=SendUsernameForm, post_reset_redirect=None):
if post_reset_redirect is None:
post_reset_redirect = reverse('auth.user.registration.views.username_is_send')
if request.method == "POST":
form = send_username_form(request.POST)
if form.is_valid():
opts = {}
opts['use_https'] = request.is_secure()
opts['email_template_name'] = email_template_name
opts['request'] = request
form.send_mail_now(**opts)
return HttpResponseRedirect(post_reset_redirect)
else:
form = send_username_form()
return render_to_response(template_name, {
'form': form,},
context_instance=RequestContext(request))
def username_is_send(request, template_name='tmp/username.html'):
return render_to_response(template_name, context_instance=RequestContext(request))
Thanks!
Craphunter
You need to store the state somehow in order to pass parameters through a redirect. Multiple possibilities:
Store the mail address in the session, then read in the session variable again in the username_is_send view.
Use a GET parameter to pass the mail address.
And it's "pass", not "path". And the "def" is called (view) function.

Categories

Resources