Re: Decorator function argument woes

2018-05-07 Thread Peter of the Norse
Yes, but that’s because all of the arguments are optional.  That way you can do:

@login_required
def my_view(request):
…

@login_required(login_url=“/login/“)
def other_view(request):
…

- Peter of the Norse

> On Apr 30, 2018, at 12:24 AM, Mike Dewhirst  wrote:
> 
>> On 30/04/2018 3:35 PM, Stephen J. Butler wrote:
>> @login_required doesn't take a test function. You need to use 
>> @user_passes_test directly.
> 
> Thank you Stephen.
> 
> I thought I'd start another thread to ask about my use case of being able to 
> require login or not depending on whether the content needed a login or not 
> ... but ... just as I was thinking about it I had another look at 
> login_required and thought I'd ask one more question.
> 
> Its signature is ...
> 
> def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, 
> login_url=None):
> 
> Are you saying that function is the the view function?
> 
> M
> 
>> 
>> On Mon, Apr 30, 2018 at 12:26 AM, Mike Dewhirst > > wrote:
>> 
>>I'm pretty sure this is a not-understanding-python problem rather
>>than a Django problem but here goes ...
>> 
>>In a (FBV) view I'm trying to pass a function in to the
>>login_required decorator. My function is called 'is_login_needed'
>>and I want the login_required decorator to effectively switch
>>itself off if login is not needed.
>> 
>>The scenario is on-line training and if the training instruction
>>has questions with scores then obviously the user needs to login.
>>On the other hand if it is simply a demonstration video or plain
>>blah with no questions/answers/scores it should be viewable by
>>anyone whether they are logged in or not.
>> 
>>My function goes like this and the docstring reveals my
>>understanding ...
>> 
>>def is_login_needed(user):
>> 
>>""" the login_required decorator has a user_passes_test()
>>function as
>> 
>>its first arg. If it returns True, no login is required.
>>user.login_here
>> 
>>is always set equal to the selected course.login_needed. For
>>courses
>> 
>>needing a login, self.is_authenticated must be True. For
>>courses not
>> 
>>needing a login, self.is_authenticated may return anything but
>>this
>> 
>>function passed to the login_required decorator must return True
>> 
>>"""
>> 
>>if not user.login_here:
>> 
>>return True
>> 
>> 
>>So   the problem is that manage.py runserver reports an attribute
>>error saying the function (presumably mine) does not have an
>>attribute 'user' ... like this ...
>> 
>>   File
>>"C:\Users\mike\envs\xxct3\train\course\urls.py", line 8, in 
>> 
>>from .views import (finished_course_view, course_view, index_view,
>> 
>>  File "C:\Users\mike\envs\xxct3\train\course\views.py", line 172,
>>in 
>> 
>>def course_view(request, pk=None, slug=None):
>> 
>>  File
>>
>> "C:\Users\mike\envs\xxct3\lib\site-packages\django\contrib\auth\decorators.py",
>>line 22, in _wrapped_view
>> 
>>if test_func(request.user):
>> 
>>AttributeError: 'function' object has no attribute 'user'
>> 
>>If that 'function' is my 'is_login_needed' function I'm inclined
>>to think it obviously has a 'user' attribute.
>> 
>>Here is the contrib.auth.decorators.login_required source
>>(preceded by user_passes_test which it calls) from Django 1.11
>> 
>>My reading of the following  is that my own 'is_login_needed'
>>function passed in via @login_required(is_login_needed,
>>login_url='login') as the first positional argument is then passed
>>to the 'user_passes_test' decorator as 'test_func'  ...
>> 
>> 
>>def user_passes_test(test_func, login_url=None,
>>redirect_field_name=REDIRECT_FIELD_NAME):
>> 
>>"""
>> 
>>Decorator for views that checks that the user passes the given
>>test,
>> 
>>redirecting to the log-in page if necessary. The test should
>>be a callable
>> 
>>that takes the user object and returns True if the user passes.
>> 
>>"""
>> 
>>def decorator(view_func):
>> 
>>@wraps(view_func, assigned=available_attrs(view_func))
>> 
>>def _wrapped_view(request, *args, **kwargs):
>> 
>>if test_func(request.user):
>> 
>>return view_func(request, *args, **kwargs)
>> 
>>path = request.build_absolute_uri()
>> 
>>resolved_login_url = resolve_url(login_url or
>>settings.LOGIN_URL)
>> 
>># If the login url is the same scheme and net location
>>then just
>> 
>># use the path as the "next" url.
>> 
>>login_scheme, login_netloc =
>>urlparse(resolved_login_url)[:2]
>> 
>>current_scheme, current_netloc = urlparse(path)[:2]
>> 
>>if 

Re: while going through the django tutorial i commited a error and because of which i cannot execute the python manage.py runserver. The errors are attached below

2018-05-07 Thread Jani Tiainen
Also next time please post exceptions as a text. Images are really hard to
read specially on mobile device.

ma 7. toukokuuta 2018 klo 20.34 Avitab Ayan Sarmah 
kirjoitti:

> i got the error thank you. It was a simple error i forgot to put "<"
> before vote
>
> On Monday, May 7, 2018 at 10:55:51 PM UTC+5:30, Avitab Ayan Sarmah wrote:
>>
>> polls/urls.py:
>>
>> from django.urls import path
>>
>> from . import views
>>
>> app_name = 'polls'
>> urlpatterns = [
>> # ex: /polls/
>> path('', views.index, name='index'),
>> # ex: /polls/5/
>> path('/', views.detail, name='detail'),
>> #ex: /polls/5/results/
>> path('/results/', views.results, name='results'),
>> #ex: /polls/5/vote/
>> path('int:question_id>/vote/', views.vote, name='vote'),
>> ]
>>
>>
>> On Monday, May 7, 2018 at 10:54:16 PM UTC+5:30, Dylan Reinhold wrote:
>>>
>>> Share your polls/url.py it's saying it cant find the url named "vote" in
>>> your polls.
>>>
>>> Dylan
>>>
>>> On Mon, May 7, 2018 at 10:14 AM, Avitab Ayan Sarmah 
>>> wrote:
>>>
 Detail.html:

 {{ question.question_text }}

 {% if error_message %}{{ error_message }}{%
 endif %}

 
 {% csrf_token %}
 {% for choice in question.choice_set.all %}
   >>> value="{{ choice.id }}" />
   {{ choice.choice_text }}
 
 {% endfor %}
 
 

 --
 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...@googlegroups.com.
 To post to this group, send email to django...@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/aab94309-a65f-49ec-bd4d-bb07bf5457a7%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/6176e606-7dbe-4a39-bd05-fc879482663c%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/CAHn91oekr%3DRnKmb6ZCDpWgL5iBH5KpDmEFB4Jv%2BVVihEaRaJzg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: unexpected JSONField behavior clarification - key value pairs initially loaded integers, then saved down as strings into the database

2018-05-07 Thread Oliver Zhou
Thanks for the reply! I accidentally posted this twice. Still seems like it 
should raise some sort of warning or exception when you're trying to edit 
both an integer key and a string key at the same time, instead of just 
allowing for seemingly non-deterministic data loss.

On Sunday, May 6, 2018 at 7:58:00 AM UTC-7, Peter of the Norse wrote:
>
> This is actually part of the JSON specification 
> .  All of the keys must be 
> strings.  If you put in an int or float, it will automatically convert it, 
> but that is something you have to watch out for since it can lead to data 
> loss.
>
> >>> import json
> >>> test = {4: 5, '4': 'a'}
> >>> json.loads(json.dumps(test))
> {'4': 5} 
>
> On Apr 24, 2018, at 6:06 PM, Oliver Zhou  > wrote:
>
> Using Django 1.11, Python 2.7, PostGres, and JSONField I'm getting 
> behavior I don't expect.
>
> It seems when JSONField values are initially saved/accessed in memory, 
> they're python "integers", but when saved to the database and then 
> re-accessed, the values are now in string format.
>
> *What about how Django handles JSONField string vs integers am I missing 
> here?* Whats the best way to get deterministic behavior here? 
> Am I supposed to always, say, use json.dumps to work with this stuff in 
> string format? Should I be converting all JSONField values before trying to 
> access any values?
>
> *Example 1 - Basic Problem*
>
> Example Model :
> class ExampleModel(models.Model): 
> external_ids = JSONField(blank=True, null=True, default=dict)
>
> My code:
> new_example = ExampleModel.objects.create()
> new_example.external_ids[1234] = {}
> print(new_example.external_ids)
>
> Output looks like this : 
> {1234: {}}
>
>
> I can now access values stored like this :
> In [140]: new_example.external_ids[1234]
> Out[140]: {}
>
> I will now then save the object to the database
> new_example.save()
>
> However, when I go back and try to access the fields again, all integer 
> values in the JSON have been converted into strings, and I cannot access 
> them anymore without getting a KeyError
> reload_example = ExampleModel.objects.get(id=1) 
> reload_example.external_ids[1234] 
> In [144]: reload_example.external_ids[1234]
> ---
> KeyError  Traceback (most recent call last
> )
>  in ()
> > 1 reload_example.external_ids[1234]
> KeyError: 1234
>
>
> By this time, the integer 1234 has been converted into a string that looks 
> like something like the following :  *Why is this?*
> In [147]: print(reload_example.external_ids)
> Out[147]: {u'1234': {}}
>
> Therefore, at this time, I have to do this to access the fields : *Whats 
> the correct way to handle this?*
> In [160]: c2.external_ids[str(1234)]
> Out[160]: {}
>
> *Example 2 - Non-deterministic Merge Problem - *
> In fact, this creates some other interesting problems for me too, where 
> integer keys and string keys get merged together in uncertain order as well.
>
> I create a new model - and save some key value pairs down into the 
> JSONField:
> In [164]: example2 = ExampleModel.objects.create()
> In [165]: example2.external_ids
> Out[165]: {}
> In [166]: example2.external_ids[1234] = {'567': '890'}
> In [167]: example2.save()
> In [169]: example2.external_ids
> Out[169]: {1234: {'567': '890'}}
>
> Then I try to reload the model back into memory :
>
> In [170]: reload_example2 = ExampleModel.objects.get(id=2)
> In [171]: reload_example2.external_ids
> Out[171]: {u'1234': {u'567': u'890'}}
>
> Then I try to save a new set of values for key 1234:
>
> In [172]: reload_example2.external_ids[1234] = {'890': '567'}
> In [173]: reload_example2.external_ids
> Out[173]: {1234: {'890': '567'}, u'1234': {u'567': u'890'}}
>
> Now it seems like both key value pairs are loaded into memory - just the 
> older one is tracked by a string-ified key, and the new values are tracked 
> by an integer-based key
> In [174]: reload_example2.save()
>
> Then I save and reload the model again - it seems uncertain which values 
> actually got saved down - *whats the expectation here?*
> In [175]: reload_example3 = ExampleModel.objects.get(id=2)
> In [176]: reload_example3.external_ids
> Out[176]: {u'1234': {u'890': u'567'}}
>
>
> Thanks!
> Oliver
>
> -- 
> 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...@googlegroups.com .
> To post to this group, send email to django...@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/ae8000bd-e7cb-44a3-9eb6-2c5a3926861e%40googlegroups.com
>  
> 

Re: looking for the right way to work with JSONField - behavior inconsistent between strings and integers

2018-05-07 Thread Oliver Zhou
Thanks for the clarification -> although it seems like it should actually 
raise a warning or exception to prevent non-deterministic data errors when 
people aren't aware.

Oliver

On Tuesday, April 24, 2018 at 8:20:53 PM UTC-7, Simon Charette wrote:
>
> Hello Olivier,
>
> Since JSON objects can only have string keys[0] I'd suggest you always use 
> strings
> as dict keys on the Python side to avoid having to deal with both cases.
>
> Another solution would to subclass JSONField to override the from_db_value 
> method
> to turn keys into integer when retrieved from the database.
>
> Cheers,
> Simon
>
> [0] 
> https://stackoverflow.com/questions/9304528/why-json-allows-only-string-to-be-a-key#9304820
>
> Le mardi 24 avril 2018 20:21:13 UTC-4, Oliver Zhou a écrit :
>>
>> Using Django 1.11, Python 2.7, PostGres, and JSONField I'm getting 
>> behavior I don't expect.
>>
>> It seems when JSONField values are initially saved/accessed in memory, 
>> they're python "integers", but when saved to the database and then 
>> re-accessed, the values are now in string format.
>>
>> *What about how Django handles JSONField string vs integers am I missing 
>> here? Whats the best way to get deterministic behavior here? *
>> Am I supposed to always, say, use json.dumps to work with JSON in string 
>> format? Should I be converting all JSONField fields into string or integers 
>> before trying to manipulate any values in Django?
>>
>> *Example 1 - Basic Problem*
>>
>> Example Model :
>> class ExampleModel(models.Model): 
>> external_ids = JSONField(blank=True, null=True, default=dict)
>>
>> My code:
>> new_example = ExampleModel.objects.create()
>> new_example.external_ids[1234] = {}
>> print(new_example.external_ids)
>>
>> Output looks like this : 
>> {1234: {}}
>>
>> I can now access values stored like this :
>> In [140]: new_example.external_ids[1234]
>> Out[140]: {}
>>
>> I will now then save the object to the database
>> new_example.save()
>>
>> However, when I go back and try to access the fields again, all integer 
>> values in the JSON have been converted into strings, and I cannot access 
>> them anymore without getting a KeyError
>> reload_example = ExampleModel.objects.get(id=1) 
>> reload_example.external_ids[1234] 
>> In [144]: reload_example.external_ids[1234]
>>
>> ---
>> KeyError  Traceback (most recent call 
>> last)
>>  in ()
>> > 1 reload_example.external_ids[1234]
>> KeyError: 1234
>>
>>
>> By this time, the integer 1234 has been converted into a string that 
>> looks like something like the following :  *Why is this?*
>>
>> *In [147]: print(reload_example.external_ids)Out[147]: {u'1234': {}}*
>> Therefore, at this time, I have to do this to access the fields : *Whats 
>> the correct way to handle this?*
>> In [160]: reload_example.external_ids[str(1234)]
>> Out[160]: {}
>>
>> *Example 2 - Confusing identical key conflict when model is loaded in 
>> memory with both integer and string based keys*
>> In fact, this creates some other interesting problems for me too, where 
>> integer keys and string keys get merged together in uncertain order as well.
>>
>> First, I create a new model and save some dictionary down to the JSONField
>>
>> In [164]:  example2 = ExampleModel.objects.create()
>> In [165]: example2.external_ids
>> Out[165]: {}
>> In [166]: example2.external_ids[1234] = {'567': '890'}
>> In [167]: example2.save()
>> In [169]: example2.external_ids
>> Out[169]: {1234: {'567': '890'}}
>>
>>
>> *Then I reload the model *
>>
>> In [170]: reload_example2 = ExampleModel.objects.get(id=2)
>> In [171]: reload_example2.external_ids
>> Out[171]: {u'1234': {u'567': u'890'}}
>>
>> *Then I try to replace those values : (I swapped the 567 and the 890)*
>>
>> In [172]: reload_example2.external_ids[1234] = {'890': '567'}
>>
>> *For the time being, now I have two key value pairs in memory for the 
>> same key, just that the older data falls under a string instance of the 
>> key, and both values can be accessed*
>> In [173]: reload_example2.external_ids
>> Out[173]: {1234: {'890': '567'}, u'1234': {u'567': u'890'}}
>>
>>
>> *After I save and then reload the model, I'm unsure which value gets 
>> saved over. (Although in tests it seems like the newest data wins)*
>> In [174]: reload_example2.save()
>> In [175]: reload_example3 = ExampleModel.objects.get(id=2)
>>
>>
>> In [176]: reload_example3.external_ids
>> Out[176]: {u'1234': {u'890': u'567'}}
>>
>> Thanks!
>> Oliver
>>
>

-- 
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 

Re: while going through the django tutorial i commited a error and because of which i cannot execute the python manage.py runserver. The errors are attached below

2018-05-07 Thread Avitab Ayan Sarmah
i got the error thank you. It was a simple error i forgot to put "<" before 
vote

On Monday, May 7, 2018 at 10:55:51 PM UTC+5:30, Avitab Ayan Sarmah wrote:
>
> polls/urls.py:
>
> from django.urls import path
>
> from . import views
>
> app_name = 'polls'
> urlpatterns = [
> # ex: /polls/
> path('', views.index, name='index'),
> # ex: /polls/5/
> path('/', views.detail, name='detail'),
> #ex: /polls/5/results/
> path('/results/', views.results, name='results'),
> #ex: /polls/5/vote/
> path('int:question_id>/vote/', views.vote, name='vote'),
> ]
>
>
> On Monday, May 7, 2018 at 10:54:16 PM UTC+5:30, Dylan Reinhold wrote:
>>
>> Share your polls/url.py it's saying it cant find the url named "vote" in 
>> your polls.
>>
>> Dylan
>>
>> On Mon, May 7, 2018 at 10:14 AM, Avitab Ayan Sarmah  
>> wrote:
>>
>>> Detail.html:
>>>
>>> {{ question.question_text }}
>>>
>>> {% if error_message %}{{ error_message }}{% 
>>> endif %}
>>>
>>> 
>>> {% csrf_token %}
>>> {% for choice in question.choice_set.all %}
>>>   >> value="{{ choice.id }}" />
>>>   {{ choice.choice_text }}
>>> 
>>> {% endfor %}
>>> 
>>> 
>>>
>>> -- 
>>> 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...@googlegroups.com.
>>> To post to this group, send email to django...@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/aab94309-a65f-49ec-bd4d-bb07bf5457a7%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/6176e606-7dbe-4a39-bd05-fc879482663c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: while going through the django tutorial i commited a error and because of which i cannot execute the python manage.py runserver. The errors are attached below

2018-05-07 Thread Avitab Ayan Sarmah
polls/urls.py:

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('/', views.detail, name='detail'),
#ex: /polls/5/results/
path('/results/', views.results, name='results'),
#ex: /polls/5/vote/
path('int:question_id>/vote/', views.vote, name='vote'),
]


On Monday, May 7, 2018 at 10:54:16 PM UTC+5:30, Dylan Reinhold wrote:
>
> Share your polls/url.py it's saying it cant find the url named "vote" in 
> your polls.
>
> Dylan
>
> On Mon, May 7, 2018 at 10:14 AM, Avitab Ayan Sarmah  > wrote:
>
>> Detail.html:
>>
>> {{ question.question_text }}
>>
>> {% if error_message %}{{ error_message }}{% endif 
>> %}
>>
>> 
>> {% csrf_token %}
>> {% for choice in question.choice_set.all %}
>>   > value="{{ choice.id }}" />
>>   {{ choice.choice_text }}
>> 
>> {% endfor %}
>> 
>> 
>>
>> -- 
>> 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...@googlegroups.com .
>> To post to this group, send email to django...@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/aab94309-a65f-49ec-bd4d-bb07bf5457a7%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/305cf889-c4de-4462-bb94-ce74a4980346%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: while going through the django tutorial i commited a error and because of which i cannot execute the python manage.py runserver. The errors are attached below

2018-05-07 Thread Dylan Reinhold
Share your polls/url.py it's saying it cant find the url named "vote" in
your polls.

Dylan

On Mon, May 7, 2018 at 10:14 AM, Avitab Ayan Sarmah 
wrote:

> Detail.html:
>
> {{ question.question_text }}
>
> {% if error_message %}{{ error_message }}{% endif
> %}
>
> 
> {% csrf_token %}
> {% for choice in question.choice_set.all %}
>value="{{ choice.id }}" />
>   {{ choice.choice_text }}
> 
> {% endfor %}
> 
> 
>
> --
> 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/aab94309-a65f-49ec-bd4d-bb07bf5457a7%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/CAHtg44A%2B_Gu5ib%2Bse-%2BjspX%2B%3DV6HW4U1O3uXgyZC9mDxDneSYA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding to all templates context via Middleware

2018-05-07 Thread Daniel Roseman
I still don't understand what is being repeated or why. You need to specify 
that in your settings, and you can add whatever context processors you like. 
You don't need to specify it twice, so nothing is being repeated.

-- 
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/098abd87-885a-4e86-b1c7-1114ee148771%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Custom model field data type

2018-05-07 Thread JAGADESH M
if you read this page you will get an idea. get the link below

https://stackoverflow.com/questions/20754793/django-module-object-has-no-attribute-bigintegerfield

On Mon, May 7, 2018 at 3:31 PM, 'Berry' via Django users <
django-users@googlegroups.com> wrote:

> I created a custom PositiveBigIntegerField data type in models.py, which
> is a sub class of the BigIntegerField. I registered the model where the
> PositiveBigIntegerField is used in the admin site. When I try to add een
> item using the admin site, the 'add' button raises an error, telling me
> PositiveBigIntegerField is not defined.
>
> Exception Type: NameError
> Exception Value: name 'BigIntegerField' is not defined
>
> Where and how do I import the custom data type, so it is defined in de
> admin site
>
> --
> 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/6f86a82f-a2de-438c-a559-86bed3d53a14%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/CAD66v%3DF2uuqwcbGA46n5Fs1gzjjBP%3D_H7rhp4A1VMx%3DYCp_hWA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Group by object's field and return list of object or object's id for each group

2018-05-07 Thread Aamu Padi
 class Ticket(models.Model):
...
booked_at = models.DateTimeField(default=timezone.now)
bought = models.BooleanField(default=False)

I would like to group tickets by booked day to get list of ticket or
ticket's id for each day. Something like this:

[
{
'day': datetime.datetime(2018, 5, 6, 0, 0, ...>),
'ticket_list': [1, 2, 3, 4],
},
{
'day': datetime.datetime(2018, 5, 7, 0, 0, ...>),
'ticket_list': [5, 6, 7, 8, 9],
}
]

I could group tickets by day this way and count total tickets per day,

Ticket.objects.filter(bought=True).annotate(day=TruncDay('booked_at')).values('day').annotate(c=Count('id')).order_by()

But I cannot figure out how to group by day and return ticket objects for
that day. Could you please help me solve this.

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/CAHSNPWsa_7RtWzopQdJa7NK_vzZtshMxMuPoc1Vsz9HiWu_GVA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Custom model field data type

2018-05-07 Thread 'Berry' via Django users
I created a custom PositiveBigIntegerField data type in models.py, which is 
a sub class of the BigIntegerField. I registered the model where the 
PositiveBigIntegerField is used in the admin site. When I try to add een 
item using the admin site, the 'add' button raises an error, telling me 
PositiveBigIntegerField is not defined.

Exception Type: NameError
Exception Value: name 'BigIntegerField' is not defined

Where and how do I import the custom data type, so it is defined in de 
admin site

-- 
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/6f86a82f-a2de-438c-a559-86bed3d53a14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Integrating Okta SAML2 with existing django web application

2018-05-07 Thread BIJAL MANIAR

Hi,

I want to integrate an existing django web application with Okta identity 
provider. I could find a couple of packages:
https://github.com/fangli/django-saml2-auth
https://github.com/MindPointGroup/django-saml2-pro-auth

Have started to use django-saml2-auth. Can we also configure advanced 
security issues like signatures and encryption with it. If anyone came 
across any other easy to use package, please let me know.

Thanks,
Bijal

-- 
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/b340babf-6d10-42d3-91fc-6ac077865562%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re:

2018-05-07 Thread Julio Biason
Hey Aayush,

Can you past the full error? It's kinda hard to see what's wrong with just
that description.

Aside from that, you can:

- Make sure you have your virtualenv up.
- Check the library is really installed (you can run `pip freeze` to see
which libraries you have installed in the virtualenv)
- Check the name; sometimes, the library name does not reflect the import
names.

On Sun, May 6, 2018 at 11:44 AM, Aayush Khandelwal <
aayushkhandelw...@gmail.com> wrote:

> Having error import name patterns ,I even downloaded it but still getting
> error what to do to sort it out
>
> --
> 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/CACVE4Jk%3DN%3DObdHLxt2Vj6avkywL-QxRya%
> 2BC0Bo6CHS3jTE6MiA%40mail.gmail.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Julio Biason*, Sofware Engineer
*AZION*  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101   |  Mobile: +55 51
*99907 0554*

-- 
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/CAEM7gE0ijM%3DAAQZ7EzSFW9efDjLRh0m-%2Be8hSxtPp%3Dzn64J96A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Username same as user id

2018-05-07 Thread 'Anthony Flury' via Django users
Yes - but that is very different from what you are asking - Facebook 
have a two stage form - first set the details - and then set the user 
name. Their user name isn't optional - it is always mandatory - it is 
just set at stage two.


You can assign any value to any variable you wish - this isn't a Django 
issue.


On 07/05/18 01:56, lakshitha kumara wrote:

hi anthony

Look at the facebook registration form. there are no username field 
first time user registration. but once user registered they can set 
username what they want.


Thanks

On Sunday, May 6, 2018 at 8:13:55 PM UTC+5:30, lakshitha kumara wrote:

Hello guys

Is there way to assign username same as user id if username
passing empty value on registration form. is there way to do that.

Thanks

--
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/e7eb07c0-6162-4c88-b5b4-8d1a5ee2d4b6%40googlegroups.com 
.

For more options, visit https://groups.google.com/d/optout.



--
--
Anthony Flury
email : *anthony.fl...@btinternet.com*
Twitter : *@TonyFlury *

--
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/72d11416-4530-8977-c02e-a4b316cfeca1%40btinternet.com.
For more options, visit https://groups.google.com/d/optout.


Re: Username same as user id

2018-05-07 Thread 'Anthony Flury' via Django users

Of course there is :
on the view which responds to your registration form (likely to be where 
you call authenticate) - you simply test if the username field has been 
set when the registration is posted - and if not set it to be whatever 
you want.


Or - in your custom backend you can do the same thing; basically you 
have code like this :


if not username:
    username = random_digits()

where random_digits is a function you write to generate your random user 
name/number.


Remember you will need to tell the user what number you have chosen for 
them.


This isn't really a Django issue to be honest - it is simply code.


On 07/05/18 07:22, lakshitha kumara wrote:


Hello Jeni

Thank you for your reply. yes i dealing with my own custom 
authentication backend and now i desided to use username as random 
number Instead of user id if username not set.


Thanks you

On Sunday, May 6, 2018 at 8:13:55 PM UTC+5:30, lakshitha kumara wrote:

Hello guys

Is there way to assign username same as user id if username
passing empty value on registration form. is there way to do that.

Thanks

--
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/a5474895-5676-449f-834f-4d3ab30594dd%40googlegroups.com 
.

For more options, visit https://groups.google.com/d/optout.



--
--
Anthony Flury
email : *anthony.fl...@btinternet.com*
Twitter : *@TonyFlury *

--
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/521987a2-bac7-704e-24ff-0b4c550656f4%40btinternet.com.
For more options, visit https://groups.google.com/d/optout.


Re: Adding to all templates context via Middleware

2018-05-07 Thread Bernd Wechner
Daniel,

The backend code contains:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "templates"),],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},

How is including all that in settings.py not a breach of DRY? When all you 
want is to add a context processor to that list?

By the by, as there is no way to inject this into context I am convinced as 
performance stats are not fully collected until the whole response is 
rendered. To wit I have done it by inserting stats just before  tag 
as a safe and clean bet. 

The result is here: 

https://github.com/bernd-wechner/CoGs/blob/develop/django_stats_middleware/__init__.py

Works well for me and is a good hook for inserting any kind of stats really 
that can be collected in the middleware layer.

Regards,

Bernd.

On Thursday, 3 May 2018 19:55:14 UTC+10, Daniel Roseman wrote:
>
> On Thursday, 3 May 2018 01:36:26 UTC+1, Bernd Wechner wrote:
>>
>> This interests me:
>>
>> 
>> https://teamtreehouse.com/community/django-how-can-i-retrieve-a-value-from-the-middleware
>>
>> alas no answer there, and time has changed things.
>>
>> I have checked form experience, that stuff added to response.context_data 
>> in middleware is not seen in templates (perhaps comes too late!).
>>
>> Another pager here:
>>
>> https://mlvin.xyz/django-templates-context-processors.html
>>
>> describes how to add to context used context processors. Searching the 
>> web is nightmare in this space because of how Django has changed over time 
>> and the different methods in different versions.
>>
>> The problem I have with that sample above is he's replicating code by 
>> including in settings.py:
>>
>> TEMPLATES = [
>> {
>> 'BACKEND': 'django.template.backends.django.DjangoTemplates',
>> 'DIRS': [os.path.join(BASE_DIR, "templates"),],
>> 'APP_DIRS': True,
>> 'OPTIONS': {
>> 'context_processors': [
>> 'django.template.context_processors.debug',
>> 'django.template.context_processors.request',
>> 'django.contrib.auth.context_processors.auth',
>> 'django.contrib.messages.context_processors.messages',
>> # our custom context processor
>> 'utils.context_processors.active_shows',
>> ],
>> },
>> },]
>>
>>
>> most of which is standard. Is there not a much more DRY way to add a 
>> context processor? Would this work?
>>
>> TEMPLATES = [
>> {
>> 'OPTIONS': {
>> 'context_processors': [
>> 'utils.context_processors.active_shows',
>> ],
>> },
>> },]
>>
>>
>> Probably not at a guess.  But is there a nicer way to do it?
>>
>> Another way might be to patch the response.content in the middleware but 
>> that too seems messy.
>>
>> Surely there's a nice way for middlware to take some timing stats around 
>> the:
>>
>> response = self.get_response(request)
>>
>> invocation that typically returns a response (and which I presume has 
>> already been through the whole template processing and has finalised 
>> response.content by the time it's done, so taking a timestamp after it's 
>> done is easy but making the result available in the templates via a context 
>> variable like {{execution_time}} would seem structurally impossible if 
>> context processing has already happened. 
>>
>> I can only imagine a trick using another context key like 
>> %%execution_time%% say and replacing that response.content.
>>
>> I wonder if I'm on the right track here? Or if someone has a better idea?
>>
>> Regards,
>>
>> Bernd.
>>
>>
>
> I don't understand what you mean about "repeating code" in settings.py. 
> There's no need to repeat anything; that TEMPLATES setting replaces what's 
> there already. You just need to edit the existing value and add that extra 
> context processor.
> --
> DR.
>

-- 
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/1a7977fa-4022-4ebe-ad8f-cda88f49cfe6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Username same as user id

2018-05-07 Thread lakshitha kumara

Hello Jeni 

Thank you for your reply. yes i dealing with my own custom authentication 
backend and now i desided to use username as random number Instead of user 
id if username not set.

Thanks you

On Sunday, May 6, 2018 at 8:13:55 PM UTC+5:30, lakshitha kumara wrote:
>
> Hello guys 
>
> Is there way to assign username same as user id if username passing empty 
> value on registration form. is there way to do that.
>
> Thanks
>

-- 
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/a5474895-5676-449f-834f-4d3ab30594dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Username same as user id

2018-05-07 Thread Jani Tiainen
Hi,

Have you considered using custom user with custom authentication backend to
handle authentication?

On Mon, May 7, 2018 at 3:56 AM, lakshitha kumara 
wrote:

> hi anthony
>
> Look at the facebook registration form. there are no username field first
> time user registration. but once user registered they can set username what
> they want.
>
> Thanks
>
> On Sunday, May 6, 2018 at 8:13:55 PM UTC+5:30, lakshitha kumara wrote:
>>
>> Hello guys
>>
>> Is there way to assign username same as user id if username passing empty
>> value on registration form. is there way to do that.
>>
>> Thanks
>>
> --
> 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/e7eb07c0-6162-4c88-b5b4-8d1a5ee2d4b6%40googlegroups.com
> 
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Jani Tiainen

- Well planned is half done, and a half done has been sufficient before...

-- 
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/CAHn91ofyJrq%3D3Tu%2B7aFiggA2mEdSgkHKdSN9HrEh%2BUqJtm_ubw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.