Re: Dynamic querysets?

2016-12-16 Thread orzodk
In your single view that processes every state I would define the
queryset you're interested in:

state_infos = {
# 'Explicit is better than implicit.'
'AK': Jurisdiction.objects.get('Alaska'),
'AL': Jurisdiction.objects.get('Alabama'),
}

Then in the view:

try:
state_data = state_infos[twodigit]
except KeyError:
# something not defined in your dict was entered.
from django.contrib import messages
messages.error(request, "That state hasn't been accepted into the "
   "union yet. Try again.")
return HttpResponseRedirect(my_index)


To answer your question more directly, I think what is happening is
you're confusing your variables with your strings.

for k, v in statedict:
  if k:
print('v')

This will return 'v' for everything because v is a string. What you
probably meant to do is print the value of each element.

for k, v in statedict:
  if k:
print(v)



Hope that helps

- Orin
Malik Rumi  writes:

> I already got one fast and helpful answer today, so I’m going to be greedy 
> and press my luck.
>
> I have this website. Each state has their own home/landing page off the 
> site’s main page, and from there you will be able to get detail pages about 
> various tidbits about the state of your choice. I have implemented this with 
> a urlconf that looks for the state’s 2 digit postal name:
>
>  url(r'^(?P[A-Z]{2})', include('bench.urls', namespace=twodigit)),
>
> It will come as no surprise that the views and templates associated with each 
> state are identical. However, in order to be DRY, I wanted the view to take 
> the twodigit argument from the url and call the right state’s queryset. To 
> this end, I created a dict
>
> {'AK': 'Alaska',
> 'AL': 'Alabama',
> 'AR': 'Arkansas',
> ...etc…}
>
> naively thinking I would be able to do something like
>
> for k,v in statedict:
>  if twodigit == k:
>  state = Jurisdiction.objects.get(v)
>
> However, this does not work. I’m not sure why. Here are some of the various 
> results I’ve gotten as I tried tweaking it:
>
> for k,v in statedict:
>  if 'VA' == k: # I was thinking of this as almost a default value
>  state = Jurisdiction.objects.get(v)
>
> However, this gets an unbound local error because of the scope, and I don’t 
> know how to assign the variable so that it is accessible outside the scope of 
> the for loop.
>
> k='NE'
> print(v)
> k=="US"
> print(v)
>
> returned
>
> U
> U
>
> Clearly, there is no ‘U’ in Nebraska, so I don’t know what happened there.
>
> This works
>
> print(statedict['US'])
> (aishah) malikarumi@Tetuoan2:~/Projects/aishah/jamf35$ python statedict.py
> United States
>
> But this does not
>
> File "statedict.py", line 63, in 
>  if statedict['k']:
> KeyError: 'k'
>
> And this
>
> for k, v in statedict:
>  if k:
>  print('v')
>
> Gets me a ‘v’ for every state.
>
> Variations on
>
> Jurisdiction.objects.filter(statedict[’v']) and
>  Jurisdiction.objects.filter(name='v’)
>
> also failed, and nothing I have found on the internet has helped. Ideas?
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/8bc1bfcb-4de5-45c9-b447-d1290e20c4d7%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8737hn9y7y.fsf%40ioa48nv.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django Password Validation

2016-05-18 Thread orzodk
Arun S  writes:

> Hi ,
>
> I have a small issue with the Validation of Passwords in the Change Password 
> Page.
>
> Due to certain different requirements, i have written my own Custom Forms for 
> Change Password.
>
> Now in this, I would want to first Validate the Old Password Field with the 
> Current Users Password.
>
> The Problem i am facing here is that the OldPassword Field provides me a 
> Password in Raw String Format.
> But  the user.password returns a Hashed Output of the Users Password
>
> And for obvious Reasons, the Validation fails between OldPassword and the 
> User.Password.
>
> In many forums i checked that the Reverse way to get the Passed from the 
> Hashed Values is not possible.
> So my only way to do this validation is through Encrypting the OldPassword 
> and then Comparing the Hash.
> But i am not sure how to do that.
>
> Can some one please tell me how is this possible to achieve and what are the 
> Apis that i can use to get the Password to be compared.
>
> Arun.

The easiest way is to authenticate the user using the password from the
form. It will return the user object if the password was correct, or
None if it wasn't.

For more information and an example see the docs:
https://docs.djangoproject.com/en/1.9/topics/auth/default/#auth-web-requests

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/m2lh377qt0.fsf%400zb9ja0m9c.com.
For more options, visit https://groups.google.com/d/optout.


Re: Scaling Django

2016-02-03 Thread orzodk

While optimizing the code will bring you improvements and you shouldn't
stop doing this, for the most part (as noted from Rafael's resources)
you should update your architecture to support Django in scaling.

As you mentioned, instead of hitting the DB for every multi-second API
call you scale it by caching results so they aren't recalculated on demand.


Microservices are something you should work toward by refactoring rather
than rewriting as that is a really great way to kill a start up.

http://steveblank.com/2011/01/25/startup-suicide-%E2%80%93-rewriting-the-code/

"Rafael E. Ferrero"  writes:

> Maybe I don't understand you very well, and for shure you have a very specific
> problem to solve... but... do you read something of this?
>
> http://blog.disqus.com/post/62187806135/scaling-django-to-8-billion-page-views
> https://www.digitalocean.com/community/tutorials/how-to-scale-django-beyond-the-basics
> https://highperformancedjango.com/
> http://talks.caktusgroup.com/djangocon/2013/scaling/#slide17
> https://docs.djangoproject.com/en/1.8/faq/general/#does-django-scale
>
> Rafael E. Ferrero
>
> 2016-02-03 12:30 GMT-03:00 Joshua Pokotilow :
>
> At the startup where I work, we've written a lot of our server code in
> Django. So far, we've adopted a "build it fast" mentality, so we invested
> very little time in optimizing our code. A small amount of load testing 
> has
> revealed our codebase / infrastructure as it stands today needs to run
> faster and support more users.
> 
> We recently hired some new engineers who are extremely skeptical that we
> should optimize our existing code. Their main concerns are:
> 
> - We need to move to a service-oriented infrastructure because Django is 
> too
> monolithic (monolithic = technology lock-in & difficult to troubleshoot)
> - It's too easy to write slow queries using the Django ORM
> - It's hard to hire Django engineers
> - While Instagram and DISQUS use Django to service large numbers of 
> people,
> they don't use it for any serious backend work
> 
> After having worked with Django for the last 3 years, I'm a big believer 
> in
> it, and I believe it would scale. To defend my position, I've pointed out 
> to
> my colleagues that it's easy to identify bottlenecks with tools like the
> Django Debug Toolbar and Yet Another Django Profiler. With my colleagues
> present, I've isolated and fixed significant speed problems inside of a 
> few
> hours. I don't believe the Django ORM is inherently bad, although I do 
> think
> that coders who use it should Know What They're Doing. Finally, I've
> referenced blog entries that talk about how Instagram and Disqus use 
> Django
> on the backend for backend-y tasks.
> 
> Despite my best efforts, my colleagues are still pushing to have us 
> rewrite
> large portions of our infrastructure as separate services before we try to
> fix them. For example, we have one slow REST endpoint that returns a
> boatload of user data, and so there's talk about using a new microservice
> for users in lieu of our existing Django models. Even if we are able to 
> fix
> bottlenecks we encounter in a timely fashion, my colleagues fear that 
> Django
> won't scale with the business.
> 
> 
> 
> I'm writing this post to garner additional evidence that Django will 
> scale.
> Anything compelling (and preferably not obvious) that would help shed some
> light on Django's ability to scale would be *greatly* appreciated, as it's
> very difficult for me to defend my position that Django is a viable
> long-term solution without solid evidence to back up my claims. It 
> certainly
> doesn't help that I don't have any experience scaling Django myself!
> 
> 
> Thank you.
>
> -- 
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> 
> https://groups.google.com/d/msgid/django-users/83968c41-d415-4189-b33b-9f99b10b1c41%40googlegroups.com
>.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/m2k2mlpvhe.fsf%40ip-192-168-1-5.us-west-