Sunday, May 31, 2015

Re: django - class based view store post data

James,

Thanks for the response. I followed your instructions and took out the overriding of post and get methods.
However, as I am testing the app using curl, I ended up with CSRF verification failed. Request aborted.

- Shekar 

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2c7bfabe-da05-48ec-88b6-ae999ea51f46%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Django ORM Interaction between order_by(), distinct(), and filter()

I have a question about how order_by(), distinct(), and filter() interact. This question is applicable only to the PostgreSQL backend since the Queryset is constructed by passing filed arguments to distinct(). https://docs.djangoproject.com/en/1.8/ref/models/querysets/#distinct In the other backends, distinct() accepts no arguments.

I'll illustrate my question with an example model. Here's a link to the code snippet: https://gist.github.com/anonymous/4111b718dbef264fb339

from django.db import models
 
class Person(models.Model):
SEX_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
name = models.CharField(max_length=255)
sex = models.CharField(max_length=255, choices=SEX_CHOICES)
city = models.CharField(max_length=255)
height = models.DecimalField(max_digits=10, decimal_places=2)
 
def __unicode__(self):
return self.name

I am interested in queries about the tallest person in a city. Specifically, 1) the tallest female in each city, and 2) the tallest person in cities where a female is the tallest. I am able to write the first query using the ORM, but not the second query.

import django
django.setup()
 
from testapp.models import Person
 
# Get a list consisting of the tallest female in each city.
tallest_females = Person.objects.filter(sex='F').order_by('city', '-height').distinct('city').values_list('pk', flat=True)
print tallest_females.query
# This outputs:
#
# SELECT DISTINCT ON ("testapp_person"."city") "testapp_person"."id"
# FROM "testapp_person"
# WHERE "testapp_person"."sex" = F
# ORDER BY "testapp_person"."city" ASC, "testapp_person"."height" DESC
 
# Get a list consiting of females who are the tallest (among all people) in
# their respective cities.
# How do I get this list using the ORM? Note that I am having resort to
# filtering results in Python.
tallest_people = Person.objects.order_by('city', '-height').distinct('city').values_list('pk', flat=True)
females_who_are_tallest_in_their_city = [ p for p in tallest_people if (p in tallest_females) ]
 
# Ideally, I'd like to write:
# Person.objects.order_by('city', '-height').distinct('city').filter(sex='F').values_list('pk', flat=True)

What's a way to compute the results of the second query fully in the database, without resorting to Python code?

Thanks,
Suriya

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/844b90e2-d312-48b9-8239-150ae821d3e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Problem in updating database models.

I am using Django 1.8 to build a demo project.Inside my project I have one app 'Book'.Book app initially had this model - 

class Book(models.Model):
      title = models.CharField(max_length=200)
      author = models.TextField()
      pub_date = models.DateTimeField('date published')

I applied makemigrations and then filled some data through my frontend website.Now Book model has 3 rows. Now I wanted to add another field to Book model like this  -

class Book(models.Model):
      title = models.CharField(max_length=200)
      author = models.TextField()
      pub_date = models.DateTimeField('date published')
       thumbnail
= models.FileField(upload_to=get_upload_file_name)

In fear of losing my existing data I dumped my data in a json file using python manage.py dumpdata command. Now what are the next steps in this?I mean now I want to empty the whole book table.Then apply makemigrations because I added one field.Also will I have to add default to thumbnail field when applying migrations?
After that I want to populate the table again from an existing json file.

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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c7e8b057-3aed-43f9-815e-3b225da6705f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: debugging with pycharm

Thx,that works.

On Tuesday, April 23, 2013 at 1:58:51 AM UTC+8, Nikolas Stevenson-Molnar wrote:
I assume you're running it in the debugger? Make sure you regular server isn't still running. Also, make sure your break point is on a line a line which does something (e.g., not on a class/function definition, empty line, etc.)

_Nik

On 4/21/2013 10:14 PM, Mark Lybrand wrote:
I can't get PyCharm to break at breakpoints when debugging a django app. what is the magic incantation i neec to invoke to make this happen regardless of where i am in code (views, urls, models, templates, etc)

--
Mark :)
--
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 http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/34902dfe-9bd4-4a4e-a70d-38dff0e4ff26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Need some suggestions on picking up the best combination

Guys,

I am working on a marketplace like project and currently am evaluating the possibilities of integration.

What I am looking for:
An e-commerce which also works well with a CMS
A phpBB style forum which also work well with other components and use same login.  forum can be simple.
Friendly to mobile app development

Here are the options that I think they may work and the questions:

Option a:
e-commerce: oscar
CMS: Django-CMS
Forum: pyBBM
payment system -- stripe  (django-stripe, does it work with oscar?)


Option b:
e-commerce: Cartridge
CMS: Mezzanine
Forum: pyBBM
payment system -- stripe  (django-stripe)


The Wagtail seems promising as a CMS but I could not find anything related to integrating with e-commerce/payment/forum etc.

Any comments on above options appreciated.  This would help me decide the right way to go since once we commit, we would need to stick to the decision for long time.  The project is a longterm project and could grow to become a large website.

Anyone know a live django based website which uses above three components? (i.e. e-commerce, CMS and forum)

Thanks,
William

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/17c8fb15-528d-4894-a109-52d5641f06f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: django - class based view store post data

I would remove the code entirely for the post() and get(). As it stands, your create() method is never being called via post() since it is normally called by CreateAPIView. From a brief look at the DRF source, the reason you are getting the error that you are is because you are calling self.get_object() within the post() method. Since you are trying to create an object, and create() has not yet been called, there is no object to retrieve. The get_object() used by the *APIView's expects the object to be identified via a URL kwarg (referenced by self.lookup_field), not via attributes that have been posted.

TL;DR; Remove all of the code that you're using to override the methods, with the exception of the create() method (see below). Unless you are doing something outside of the norm (if you don't know what that means, you probably aren't), there shouldn't be a need to override them.

I just found this site, which is like Classy CBV's, except for the DRF, which is amazing: http://www.cdrf.co/3.1/rest_framework.generics/CreateAPIView.html

My guess would be to override create() in your view and add the owner to request.data, something along these lines, (Full disclosure: I've never done this, so YMMV):

def create(self, request, *args, **kwargs):
    request.data['owner'] = request.user
    return super(ViewName, self).create(request, *args, **kwargs)

You may need to play with request.data to figure out the structure and how to modify it. 

If the serializer throws an error complaining about 'owner' being a read-only field, you'll probably need to change the type of field. Just make sure to override update() in a similar as well to keep the user from changing that field.

I don't have a working setup with DRF to test, so YMMV.

For better help, though, you should post your question on the DRF mailing list, rather than here in the Django user list: https://groups.google.com/forum/?fromgroups#!forum/django-rest-framework

-James

On Sun, May 31, 2015 at 10:03 AM, Shekar Tippur <ctippur@gmail.com> wrote:
James,

Thanks for responding. 

I have changed the view to:

class AddToUserProfile(generics.CreateAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
queryset = UserPrefs.objects.all()
serializer_class = UserPrefSerializer
lookup_field = 'pk'
def get(self, request, *args, **kwargs):
return HttpResponse('Hello world')

def post(self, request, *args, **kwargs):
#return self.get(request, *args, **kwargs)
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
def create(self, serializer):
serializer.save(owner=self.request.user)

I get an error: Expected view AddToUserProfile to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69f2dbec-06a4-4338-953f-39f1f38576c1%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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXvfyaxnE9ixB4zmCT8zSOQ4WvvPseY9soyXbi6cpA%2BXw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: icontains case-sensitive on MySQL

On 1/06/2015 5:57 AM, Jake Gordon wrote:
> There needs to at least be a warning in the documentation.

https://docs.djangoproject.com/en/1.8/ref/databases/#collation-settings

I spent a
> lot of time debugging only to find this was a problem with Django.
>
>
> On Thursday, October 16, 2008 at 6:56:07 PM UTC-4, Malcolm Tredinnick wrote:
>
>
> On Thu, 2008-10-16 at 11:15 -0700, AndyB wrote:
> > Well - someone on #Django told me to check encoding settings and
> I did
> > but I failed to read the following:
> > http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html
> <http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html>
> >
> > Using binary collation changes the default sensitivity of string
> > comparisons.
> >
> > Is this not something that Django should be abstracting away?
>
> No. You set the database collation and Django respects that. We can't do
> anything else, since the comparison takes place at the database level.
>
> The other one is that we (particularly Karen Tracey, myself and a couple
> of other people) spent a lot of time trying to see if we could make
> things like that work regardless of collation (particularly
> case-sensitive matching with the default utf_*_ci collations), but it's
> really, really hard. Really hard. There are just too many side-effects
> and, ultimately, it becomes hard to understand what's going on in a
> practical matter, since we almost always defer to the database on
> matters on how a particular SQL query will be executed and this would be
> one situation when we didn't (sometimes). So it's entirely left up to
> how you configure your database. One of the trade-offs the developer
> chooses to make when selecting MySQL.
>
> Regards,
> Malcolm
>
>
> --
> 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+unsubscribe@googlegroups.com
> <mailto:django-users+unsubscribe@googlegroups.com>.
> To post to this group, send email to django-users@googlegroups.com
> <mailto:django-users@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d8bbe872-8182-421d-a1bf-eb9ea9dc6035%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/d8bbe872-8182-421d-a1bf-eb9ea9dc6035%40googlegroups.com?utm_medium=email&utm_source=footer>.
> 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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/556B96BC.9000503%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Re: generating pdf report with django-easy-pdf


You need to add the get method this:

 def get(self, request, *args, **kwargs):
    """
Handles GET request and returns HTTP response.
"""
context = self.get_context_data(**kwargs)
#data: dic, array, etc.
values
= [1,2,3,4,5]
# add the data in the context
context["values"] = values

return self.render_to_response(context)


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/afe2127d-4b05-4584-bbb8-92a3435d78ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: icontains case-sensitive on MySQL

There needs to at least be a warning in the documentation.  I spent a lot of time debugging only to find this was a problem with Django.


On Thursday, October 16, 2008 at 6:56:07 PM UTC-4, Malcolm Tredinnick wrote:

On Thu, 2008-10-16 at 11:15 -0700, AndyB wrote:
> Well - someone on #Django told me to check encoding settings and I did
> but I failed to read the following:
> http://dev.mysql.com/doc/refman/5.0/en/case-sensitivity.html
>
> Using binary collation changes the default sensitivity of string
> comparisons.
>
> Is this not something that Django should be abstracting away?

No. You set the database collation and Django respects that. We can't do
anything else, since the comparison takes place at the database level.

The other one is that we (particularly Karen Tracey, myself and a couple
of other people) spent a lot of time trying to see if we could make
things like that work regardless of collation (particularly
case-sensitive matching with the default utf_*_ci collations), but it's
really, really hard. Really hard. There are just too many side-effects
and, ultimately, it becomes hard to understand what's going on in a
practical matter, since we almost always defer to the database on
matters on how a particular SQL query will be executed and this would be
one situation when we didn't (sometimes). So it's entirely left up to
how you configure your database. One of the trade-offs the developer
chooses to make when selecting MySQL.

Regards,
Malcolm


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d8bbe872-8182-421d-a1bf-eb9ea9dc6035%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: problem with requests and django view

I found myself the solution. Stupid!.  The variable url was entered  without slash at the end.
Anyway thanks to all. Maybe somebody could see that if I copied the url and urls.py in the question.

Am Freitag, 29. Mai 2015 17:09:39 UTC+2 schrieb ogi:
Hi

I have problems to post some json data from a python script using requests to a django view defined as base classed view.
Thinking that problem is with permissions I also installed django rest framework to temporary allow any user to call the view.

the file with post data is looking like:

data = {'k1': 'val1', 'k2': 'val2'}
jdata = json.dumps(data)
r = requests.post(url, data=jdata, headers=headers)
print r

and the correspondign class based view registrated in urls.py:

class LoadData(APIView):

    """
    A view that can accept POST requests with JSON content.
    """
    parser_classes = (JSONParser,)
    permission_classes = (AllowAny,)

    def post(self, request, format=None):
        # pdb.set_trace()

now the method post will be not executed.
When I use put method instead of post the situation is better but also without data
in request.data or args, kwargs.

I used many times jQuery with ajax and post method in similar cases and it was always working,
so I have no idea what is the difference and what I am missing.

Any hint will be very appreciated and thanks in advance.
Ogi



--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/019e4712-c1a4-4c8b-9a2f-0dead5c9b34e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: django - class based view store post data

James,

Thanks for responding. 

I have changed the view to:

class AddToUserProfile(generics.CreateAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
queryset = UserPrefs.objects.all()
serializer_class = UserPrefSerializer
lookup_field = 'pk'
def get(self, request, *args, **kwargs):
return HttpResponse('Hello world')

def post(self, request, *args, **kwargs):
#return self.get(request, *args, **kwargs)
self.object = self.get_object()
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
def create(self, serializer):
serializer.save(owner=self.request.user)

I get an error: Expected view AddToUserProfile to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/69f2dbec-06a4-4338-953f-39f1f38576c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: CSRF verification failed when I use smart phone

I had this error when I had two Django application with the same domain and both with the same (default) CSRF cookie name. Changing the cookie name to something different solved the issue.

On 30 May 2015 22:30, "Michael Greer" <michael@tapptv.com> wrote:
We have started seeing this behavior occasionally. No code change in this area, but sometimes (esp on phones) the CSRF cookie is wrong.

Our guess is too many cookies on the domain, but it is difficult to prove this. Indeed, opening a new browser session resolves it... temporarily.

-Mike

On Tuesday, January 6, 2015 at 3:09:46 AM UTC-6, Sugita Shinsuke wrote:
Hello.

When I use Django via my smart phone Android and iOS.
The error sometimes occurred.

Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF token missing or incorrect.

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
Your browser is accepting cookies.
The view function uses RequestContext for the template, instead of Context.
In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.


I append django.middleware.csrf.CsrfViewMiddleware', of MIDDLEWARE_CLASSES in settings.py

I use
Python 2.7.5
Django 1.6.4

Anyone who know this matter, please help.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/89d20584-d3bf-4104-b642-0f519c4deb10%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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACczBULNhCDS_w4YvaUb%2Byn0MjEosq6sR6TvmSSYJ7XbxCDHDg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: django - class based view store post data

A few things to consider:

In your view, it looks like you have 'lookup_fields' instead of 'lookup_field'.

http://www.django-rest-framework.org/api-guide/generic-views/#genericapiview

From a quick read through the DRF docs, it also looks like it only requires a single str value, and you are have it surrounded by (), which may lead to confusion later.

You have overridden post() but it doesn't return anything because all of the code is commented out. That'll also throw misleading errors, if it even runs. I'm assuming that may be from you testing things though.

You may also want to look at one of the other views to inherit from, such as the CreateAPIView, which may be more appropriate and efficient for what you are trying to do, rather than just inheriting from the base view class (unless you are trying to do something crazy). It's equivalent to just inheriting from View when using CBV's, which isn't the norm.

It is also possible that you should be POSTing to a URL that contains the 'pk' of the object that you are trying to modify. If you are trying to create an object, I would suggest looking into the CreateAPIView, since the base view probably expects an object pk to work with.

I've only used DRF briefly, so there may be other issues that I haven't spotted.

-James

On May 30, 2015 11:15 PM, "Shekar Tippur" <ctippur@gmail.com> wrote:
Hello,

I am trying to use class based view to post data.
I have come across a weird issue. It is not saving data in the backend. I get an error:
Expected view AddToUserProfile to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly. 

I have searched on google to see explanation on this error but could not understand the explanation. Appreciate if someone can shed some light on this.

Here is my model:

class UserPrefs(models.Model):
Preferences = (
(1,'Likes'),
(1,'Dislikes'),
(1,'Shared'),
(1,'Rejected')
)
user = models.ForeignKey('auth.User', related_name='Hello')
name = models.ForeignKey(Stock)
value = models.CharField(max_length=20,choices=Preferences)

class Meta:
#managed = False
db_table = 'user_pref'

def save(self, *args, **kwargs):
super(Hello, self).save(*args, **kwargs)

class AddToUserProfile(generics.GenericAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
queryset = UserPrefs.objects.all()
serializer_class = UserPrefSerializer
lookup_fields = ('id')
def get(self, request, *args, **kwargs):
return HttpResponse('Hello there!')

def post(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
#eturn self.render_to_response(context)
def create(self, serializer):
serializer.save(owner=self.request.user)

Serializer code:

class UserPrefSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
model = UserPrefs
fields = ( 'owner', 'name', 'value')
depth = 1

Url.py entry:
url(r'^Hello/setPrefs', views.AddToUserProfile.as_view()),

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6dbb2532-3bc3-4108-b1c9-9d48337fb02c%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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciW0wnu__XUuCJOFV6mC8gZ%3DJg1pNpsxec6SgnRkaS-ygA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Saturday, May 30, 2015

django - class based view store post data

Hello,

I am trying to use class based view to post data.
I have come across a weird issue. It is not saving data in the backend. I get an error:
Expected view AddToUserProfile to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly. 

I have searched on google to see explanation on this error but could not understand the explanation. Appreciate if someone can shed some light on this.

Here is my model:

class UserPrefs(models.Model):
Preferences = (
(1,'Likes'),
(1,'Dislikes'),
(1,'Shared'),
(1,'Rejected')
)
user = models.ForeignKey('auth.User', related_name='Hello')
name = models.ForeignKey(Stock)
value = models.CharField(max_length=20,choices=Preferences)

class Meta:
#managed = False
db_table = 'user_pref'

def save(self, *args, **kwargs):
super(Hello, self).save(*args, **kwargs)

class AddToUserProfile(generics.GenericAPIView):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly)
queryset = UserPrefs.objects.all()
serializer_class = UserPrefSerializer
lookup_fields = ('id')
def get(self, request, *args, **kwargs):
return HttpResponse('Hello there!')

def post(self, request, *args, **kwargs):
self.object = self.get_object()
context = self.get_context_data(object=self.object)
#eturn self.render_to_response(context)
def create(self, serializer):
serializer.save(owner=self.request.user)

Serializer code:

class UserPrefSerializer(serializers.ModelSerializer):
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
model = UserPrefs
fields = ( 'owner', 'name', 'value')
depth = 1

Url.py entry:
url(r'^Hello/setPrefs', views.AddToUserProfile.as_view()),

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6dbb2532-3bc3-4108-b1c9-9d48337fb02c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: CSRF verification failed when I use smart phone

We have started seeing this behavior occasionally. No code change in this area, but sometimes (esp on phones) the CSRF cookie is wrong.

Our guess is too many cookies on the domain, but it is difficult to prove this. Indeed, opening a new browser session resolves it... temporarily.

-Mike

On Tuesday, January 6, 2015 at 3:09:46 AM UTC-6, Sugita Shinsuke wrote:
Hello.

When I use Django via my smart phone Android and iOS.
The error sometimes occurred.

Forbidden (403)
CSRF verification failed. Request aborted.
Help
Reason given for failure:
CSRF token missing or incorrect.

In general, this can occur when there is a genuine Cross Site Request Forgery, or when Django's CSRF mechanism has not been used correctly. For POST forms, you need to ensure:
Your browser is accepting cookies.
The view function uses RequestContext for the template, instead of Context.
In the template, there is a {% csrf_token %} template tag inside each POST form that targets an internal URL.
If you are not using CsrfViewMiddleware, then you must use csrf_protect on any views that use the csrf_token template tag, as well as those that accept the POST data.
You're seeing the help section of this page because you have DEBUG = True in your Django settings file. Change that to False, and only the initial error message will be displayed.
You can customize this page using the CSRF_FAILURE_VIEW setting.


I append django.middleware.csrf.CsrfViewMiddleware', of MIDDLEWARE_CLASSES in settings.py

I use
Python 2.7.5
Django 1.6.4

Anyone who know this matter, please help.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/89d20584-d3bf-4104-b642-0f519c4deb10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Migrate command does not respect options for db connections?

Context:

Django 1.7.1
PostgreSQL 9.4

I have the following database entry in settings:

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'xpto',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '5432',
'OPTIONS': {
'options': '-c search_path=xpto,public'
}
}
}

And the following model:

class ModelA(models.Model):
    att_a = models.CharField(max_length=10)

I' just created a migration with makemigrations for this app. It does not have any indication of schema, whatsoever.

When I ran the migration, with migrate, the table ended up on the public schema.

Bug?

--
George R. C. Silva
Sigma Geosistemas LTDA
----------------------------

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAGyPVTvGh7HTGGyTgj4Qu8cpxp9ddDTmBY7si4uoq9Z484%2BX8w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Upgrade to django 1.7- ImproperlyConfigured -Tastypie label duplicate? help

Were you able to solve the problem @amarshall? I have the exact same error, do let me know please!

On Tuesday, March 31, 2015 at 8:04:23 PM UTC+5:30, Filipe Ximenes wrote:
Thats strange.
Are you using pip to install dependencies? Try uninstalling everything and reinstalling again. 
Make sure INSTALLED_APPS is not being defined somewhere else in the code. 
Does the error persists if you set DEBUG = False locally?

On Tue, Mar 31, 2015 at 10:21 AM, amarshall <adria...@gmail.com> wrote:
Hi Filipe,

I have few apps installed. When I comment out the tastypie line my app builds but gets an error in production because tastypie app isn't found. Here it is:

INSTALLED_APPS = (
   
'django.contrib.admin',
   
'django.contrib.auth',
   
'django.contrib.contenttypes',
   
'django.contrib.sessions',
   
'django.contrib.messages',
   
'django.contrib.staticfiles',
   
'rest_framework',
   
# 'south',       // commented out after upgrade to Django 1.7
   
'tastypie',
   
'lokal',        #my app
)



On Tuesday, March 31, 2015 at 5:52:55 AM UTC-4, Filipe Ximenes wrote:

Can you show us your INSTALED_APPS?
It may be the case you have a lot of apps installed and is not seeing a duplicate reference to tastypie.

On Mar 29, 2015 9:43 PM, "amarshall" <adria...@gmail.com> wrote:
Hi,

 So I updated my django project from django 1.6 to 1.7.7 and when I run "python manage.py migrate" I get this error stating that I have a duplicate label.

Here's the traceback

  Creating tables...
 
Installing custom SQL...
 
Installing indexes...
Running migrations:
 
Applying tastypie.0001_initial...Traceback (most recent call last):
 
File "manage.py", line 11, in <module>
    execute_from_command_line
(sys.argv)
 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility
.execute()
 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 377, in execute
   
self.fetch_command(subcommand).run_from_argv(self.argv)
 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 288, in run_from_argv
   
self.execute(*args, **options.__dict__)
 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 338, in execute
    output
= self.handle(*args, **options)
 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor
.migrate(targets, plan, fake=options.get("fake", False))
 
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 68, in migrate
   
self.apply_migration(migration, fake=fake)
 
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 96, in apply_migration
   
if self.detect_soft_applied(migration):
 
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 140, in detect_soft_applied
    apps
= project_state.render()
 
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/state.py", line 57, in render
   
self.apps = Apps([AppConfigStub(label) for label in sorted(self.real_apps + list(app_labels))])
 
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 56, in __init__
   
self.populate(installed_apps)
 
File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 89, in populate
   
"duplicates: %s" % app_config.label)
django
.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: tastypie


Not sure what to do here. I remove tastypie from the list of INSTALLED_APPS in my settings and everything works fine. but I do need it. Any suggestions ?

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7e4cfbf8-d3e0-4e0e-a38c-b83930ffa3b6%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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5128dd5e-faa1-4c6a-9cf4-0a59f7d0bc2c%40googlegroups.com.

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



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9ddcfb50-32d4-4925-9939-feb8ceb06944%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.