Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Storgar
Oct 31, 2011

Thermopyle posted:

When you capture a group in an url regex it gets passed to the view which is the second parameter of the url function. It looks like either django or DRF is choking on this unexpected group getting passed to it.

Whatever it is that is happening exactly it doesn't matter, because you don't need to do this at all. DRF will already handle urls with or without the trailing slash.

Just put url(r'^api/', include(router.urls)), and you'll get your api root whether you visit blahblah.com/api or blahblah.com/api/ if you're using the SimpleRouter or the DefaultRouter .

Ooooh. Duh, that makes sense. I was using the parentheses in a mathematical context.

I thought that I read about the trailing slash somewhere in the documentation. Is there a case where it wouldn't automatically be redirected to the proper url regardless of slash? (I remember some times when it didn't do this, but I have no idea what settings I had...)

Adbot
ADBOT LOVES YOU

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
Ok, so I think I'm doing this wrong but I was hoping for some confirmation.

I'm writing a generic survey application that has dynamically generated forms. These forms are built off a set of questions which can store an indiscriminate data type in a given section/page/whatever.

Questions also have validators. They can use the built in validators but also a set of custom validators. One of the validators I've built tests against the value of other fields in the form. The way that I'm doing it is to subclass forms.CharField and override clean() and run_validators() to pass the form to the validator so I can test the other field against the field being validated.

I feel like this is a dangerous pattern because I know Django fields are designed so they don't know anything about the form that they're part of.

Can anyone suggest a better way to do this? I can provide more information if anything is unclear.

Typh
Apr 18, 2003

LAY EGG IS TRUE!!!
This has been tripping me up so often lately that I'm starting to wonder if I have a workflow issue or if I'm the only one using django migrations https://code.djangoproject.com/ticket/23410

How the heck are you supposed to work on/test a migration (ie data migration) if you can't reliably roll it backwards/forwards without random other apps rolling back too?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Thermopyle posted:

For my own question....has anyone dealt with handling a writable GenericForeignKey in django-rest-framework? I can get a readable hyperlinked representation like so:

Python code:
class SomeSerializer(serializers.HyperlinkedModelSerializer):
	content_object = serializers.SerializerMethodField('get_content_obj_url')

	def get_content_obj_url(self, obj):
		obj = obj.content_object

		view_name = get_view_name(obj)
		s = serializers.HyperlinkedIdentityField(source=obj, view_name=view_name)
		s.initialize(self, None)
		return s.field_to_native(obj, None)
(get_view_name(obj) is just a simple function for returning the DRF default view name of modelname-detail)

But, obviously, that doesn't work for POST'ing and today I can't think of a good way of supporting this.

To answer my own question:

Python code:
class SomeSerializer(serializers.HyperlinkedModelSerializer):
    content_object = serializers.SerializerMethodField('get_content_obj_url')

    def get_content_obj_url(self, obj):
        obj = obj.content_object

        view_name = get_view_name(obj)
        s = serializers.HyperlinkedIdentityField(source=obj, view_name=view_name)
        s.initialize(self, None)
        return s.field_to_native(obj, None)

    def restore_fields(self, data, files):
        content_object = None

        if 'content_object' in data:
            request = self.context.get('request')  
            content_object = get_object_from_url(request.DATA['content_object'])

        attrs = super().restore_fields(data, files)
        if content_object:
            attrs['content_object'] = content_object
        return attrs

    class Meta:
        model = SomeModel
        fields = ('id', 'blah', 'bloop', 'bleep', 'content_object')

def get_model_from_url(url: str):
    return resolve(urlparse(url).path).func.cls.model

def get_object_from_url(url: str):
    model = get_model_from_url(url)
    pk = resolve(urlparse(url).path).kwargs.get('pk')
    if not pk:
        return None
    return model.objects.get(pk=pk)
I messed around with overriding the create method on my ModelViewSet, but it really felt like this belonged in the serializer.

So anyway, this overriden `restore_fields` method will look up the object referenced by a hyperlink POST'ed to the content_object field and do the right thing with it. It won't allow you to POST in the browsable API, but I'm not sure how that would work anyway, as the select box normally used for related fields would have to be populated with every object from every model in your project...

It took me a lot of tracing with the PyCharm debugger to figure this out. I really wish there was something like ccbv for DRF.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I was just trying to figure out all the configured urls in my project. It can get confusing if you're using several third party apps that configure their own urls.

So, I whipped this together to print out a list of all the urls.

https://gist.github.com/dmwyatt/4ef6a34874a01d9f3ffb

Yay
Aug 4, 2007

Thermopyle posted:

I was just trying to figure out all the configured urls in my project. It can get confusing if you're using several third party apps that configure their own urls.
[...]
Not to diminish what you did, but future-you may appreciate:
`pip install django_extensions`
`python manage.py show_urls`

I've used it when coming onboard a large project I'm unfamiliar with, on more than one occasion.

ohrwurm
Jun 25, 2003

I'm working on an application that runs a few different sites using the same codebase. The differences between the sites are mostly cosmetic, but there are site-specific terms that need to be enforced e.g. site1.com/clients/ site2.com/members/ - both resolve to the same view.
The url pattern looks something like url(r'^%s/' % config.member_text, view stuff, namespace stuff)

This is fine running separate django instances since the url patterns can be set by a config setting when the instance is started.

I've been tasked with running these sites (and more to come in the future) off of a single instance. The problem is the urls are set on application start, so they can't be dynamic per request. Is there some way to make this type of setup work?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Yay posted:

Not to diminish what you did, but future-you may appreciate:
`pip install django_extensions`
`python manage.py show_urls`

I've used it when coming onboard a large project I'm unfamiliar with, on more than one occasion.

Oh, durr.

----------

New question. I'm doing a single page app with a backend API built with django-rest-framework. I'm thinking about hosting and I'm having a hard time deciding how to do it.

I serve stuff from S3 with Heroku-hosted Django all the time, but I don't think this would work with my SPA setup since my HTML and JS would be coming from the amazon domain and wouldn't work because of Same Origin Policy.

I haven't really messed with hosting static stuff directly fro Heroku, but my understanding is this is heavily discouraged because of (I guess) performance issues.

What kind of hosting are people using for Django-backed single page apps?

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!

Thermopyle posted:

Oh, durr.

----------

New question. I'm doing a single page app with a backend API built with django-rest-framework. I'm thinking about hosting and I'm having a hard time deciding how to do it.

I serve stuff from S3 with Heroku-hosted Django all the time, but I don't think this would work with my SPA setup since my HTML and JS would be coming from the amazon domain and wouldn't work because of Same Origin Policy.

I haven't really messed with hosting static stuff directly fro Heroku, but my understanding is this is heavily discouraged because of (I guess) performance issues.

What kind of hosting are people using for Django-backed single page apps?

Just because it's a single page app doesn't mean you have to serve it as a static file. Just run it as a single view out of Django/Heroku.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Ahz posted:

Just because it's a single page app doesn't mean you have to serve it as a static file. Just run it as a single view out of Django/Heroku.

Yeah, I obviously need to do some profiling, but my understanding is there are performance benefits to serving as a static file and avoiding the Django view machinery.

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!
I would bet that any performance loss from serving a single essentially static template out of Django vs. AWS would be dwarfed by the req/res cycles of your REST requests / DB hits post-initial-load.

Just a guess.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Ahz posted:

I would bet that any performance loss from serving a single essentially static template out of Django vs. AWS would be dwarfed by the req/res cycles of your REST requests / DB hits post-initial-load.

Just a guess.

Yeah, this is a good point.

duck hunt
Dec 22, 2010
I've been developing a blogging engine on Django 1.7 and I'm wondering if I'm falling into a anti-pattern / pitfall

I'm passing each blog into the template engine, and then using its pk value in the HTML id attribute.

code:
<button type="submit" id="submit-comment-{{ blog.id }}">
I use the id's to easily add event listeners.

Is using the pk value in the HTML id attribute a potential pitfall, or am I over blowing things? Is there a better way, on a page with multiple blogs, to attach event listeners to capture the event on a specific blog without doing as I have done?

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
I have a custom 500.html template that gets returned when the server shits the bed. How should I go about testing the view to make sure the template gets sent on server errors? The obvious answer is create a server error, but I've worked pretty hard to make that not possible and I'm wondering if there is an easy way to mock it or something?

Gounads
Mar 13, 2013

Where am I?
How did I get here?
Shut down your database?

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
In your unit test (you are doing those, right?) create a response object and set the code to 500 then pass it to your view.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

Blinkz0rz posted:

In your unit test (you are doing those, right?) create a response object and set the code to 500 then pass it to your view.

Yeah, the question was about my unit tests. I don't know why I didn't think of this, thank you.

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!

duck hunt posted:

I've been developing a blogging engine on Django 1.7 and I'm wondering if I'm falling into a anti-pattern / pitfall

I'm passing each blog into the template engine, and then using its pk value in the HTML id attribute.

code:
<button type="submit" id="submit-comment-{{ blog.id }}">
I use the id's to easily add event listeners.

Is using the pk value in the HTML id attribute a potential pitfall, or am I over blowing things? Is there a better way, on a page with multiple blogs, to attach event listeners to capture the event on a specific blog without doing as I have done?

If you were using Django forms, you could have the ID automatically set into the form with a hidden field widget. It's a bit odd and counter productive to parse the submit id after posting unless you plan to use the ID for something on the page itself like javascript/css. If you wanted to use the ID in those cases, you could use the form.field.id_for_label attribute to automatically spit out the id with a relevant unique html id.

duck hunt
Dec 22, 2010
Thanks for the idea. I will do give that a shot on my forms.

Even though in the code example I gave, I was showing a submit button, the {{ blog.id }} as a HTML attribute is mostly being used to allow for efficient document.getElementById() type code, and yes, like you mentioned, some CSS too.

Storgar
Oct 31, 2011
Aw poop. When I do
code:
python manage.py test
I get 0 tests run in 0.000s. I have tests.py with a class called ThoughtBasicTestCase containing a method called test_create_thought() inside. It runs this test when I type in
code:
python manage.py test blog
. Am I doing something wrong?

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

Storgar posted:

Aw poop. When I do
code:
python manage.py test
I get 0 tests run in 0.000s. I have tests.py with a class called ThoughtBasicTestCase containing a method called test_create_thought() inside. It runs this test when I type in
code:
python manage.py test blog
. Am I doing something wrong?

What version of django are you on and what does your file structure look like? Also, make sure you've got an __init__.py in the folder the tests are in.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
Django stupidly needs at least one model defined in the app for it to consider the app as something that can be tested. If your app doesn't have models you can trick it by putting a models.py file in the root of the app.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Blinkz0rz posted:

Django stupidly needs at least one model defined in the app for it to consider the app as something that can be tested. If your app doesn't have models you can trick it by putting a models.py file in the root of the app.

Is that the case with 1.7? I know they revamped how apps work...

Storgar
Oct 31, 2011
Oops, forgot to say. I'm using Django 1.7.

Folder structure is like this:
code:
/app
  /blog
    __init__.py
    tests.py
I have two models in my "blog" app. So I don't think that's the problem either... It does run the test suite correctly if I type in "python manage.py test blog".

The March Hare posted:

What version of django are you on and what does your file structure look like? Also, make sure you've got an __init__.py in the folder the tests are in.

That was it. I put an __init__.py in the "/app" folder and it works now. Thanks guys!

Yay
Aug 4, 2007

Thermopyle posted:

Is that the case with 1.7? I know they revamped how apps work...
No, it's not. An AppConfig's models_module may be None, which is part of the refactor you're talking about.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

The March Hare posted:

Yeah, the question was about my unit tests. I don't know why I didn't think of this, thank you.

As a followup to this, I just found out that django.http has a thing called HttpResponseServerError which is just an HttpResponse w/ a 500 status code by default.

Which means I can just do:

code:
class Custom500Test(TestCase):

    def test_server_error_renders_custom_500_template(self):
        response = HttpResponseServerError()

        self.assertTemplateUsed(response, '500.html')

The March Hare fucked around with this message at 18:03 on Nov 24, 2014

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

I just wanted to say this here because I know a lot of people read this thread who are just at the right stage in their journey to becoming the Bestest Web Programmer EVER...

I finally said gently caress it and I am doing all projects going forward without using any Django forms or templates, and its been pretty great.

I basically set up two projects. The Django project handles the models and provides access to them via an API built with django-rest-framework, and then in a completely separate project I develop a JS/HTML/CSS frontend.

This works great for even simple sites once you've got your mind wrapped around a javascript framework or two.

I like the clarity you get from the separation of concerns like this.

Anyway, give it a try. It's the future.

Storgar
Oct 31, 2011

Thermopyle posted:

I just wanted to say this here because I know a lot of people read this thread who are just at the right stage in their journey to becoming the Bestest Web Programmer EVER...

I finally said gently caress it and I am doing all projects going forward without using any Django forms or templates, and its been pretty great.

I basically set up two projects. The Django project handles the models and provides access to them via an API built with django-rest-framework, and then in a completely separate project I develop a JS/HTML/CSS frontend.

This works great for even simple sites once you've got your mind wrapped around a javascript framework or two.

I like the clarity you get from the separation of concerns like this.

Anyway, give it a try. It's the future.

So you handle all the navigational urls and reverse url template tags with your javascript framework? Also, does the javascript framework you're using have a name? Are they served by Django or do you have another program to do that?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Storgar posted:

So you handle all the navigational urls and reverse url template tags with your javascript framework? Also, does the javascript framework you're using have a name? Are they served by Django or do you have another program to do that?

The only urls served by Django are like api.domain.com/api/foo/1/ and api.domain.com/api/bar.

The actual site end users see has urls like domain.com/inbox and domain.com/profile. Those urls are all routed to and "created" by the javascript framework.

You can use Ember, React, AngularJS, a bunch of others to do this. I prefer React.

I'm doing something with AngularJS right now, so I'll point to examples for that...

Taken from the AngularJS tutorial:

JavaScript code:
phonecatApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/phones', {
        templateUrl: 'partials/phone-list.html',
        controller: 'PhoneListCtrl'
      }).
      when('/phones/:phoneId', {
        templateUrl: 'partials/phone-detail.html',
        controller: 'PhoneDetailCtrl'
      }).
      otherwise({
        redirectTo: '/phones'
      });
  }]);
You can wire up Angular to your Django-powered API with ngResource like this video demonstrates.

The client app can be served by the same server Django is running on if you have access to configure the server to serve those as static files. I'm currently hosting Django on Heroku and serving my frontend from Amazon S3.

No Wave
Sep 18, 2005

HA! HA! NICE! WHAT A TOOL!

Thermopyle posted:

I just wanted to say this here because I know a lot of people read this thread who are just at the right stage in their journey to becoming the Bestest Web Programmer EVER...

I finally said gently caress it and I am doing all projects going forward without using any Django forms or templates, and its been pretty great.

I basically set up two projects. The Django project handles the models and provides access to them via an API built with django-rest-framework, and then in a completely separate project I develop a JS/HTML/CSS frontend.

This works great for even simple sites once you've got your mind wrapped around a javascript framework or two.

I like the clarity you get from the separation of concerns like this.

Anyway, give it a try. It's the future.
I think you're definitely right. Do you still feel like you're getting enough out of Django, or have you been tempted to use a different framework?

xpander
Sep 2, 2004

Thermopyle posted:

I just wanted to say this here because I know a lot of people read this thread who are just at the right stage in their journey to becoming the Bestest Web Programmer EVER...

I finally said gently caress it and I am doing all projects going forward without using any Django forms or templates, and its been pretty great.

I basically set up two projects. The Django project handles the models and provides access to them via an API built with django-rest-framework, and then in a completely separate project I develop a JS/HTML/CSS frontend.

This works great for even simple sites once you've got your mind wrapped around a javascript framework or two.

I like the clarity you get from the separation of concerns like this.

Anyway, give it a try. It's the future.

I'd decided that I was going to try this with my next project, which is interaction-heavy for its main functionality, and this makes me feel better about my decision. Do you have a fallback in case JS no workie? Or is that just not a concern in TYOOL 2014/2015?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

No Wave posted:

I think you're definitely right. Do you still feel like you're getting enough out of Django, or have you been tempted to use a different framework?

I've got a compulsion to always be trying new things. However, in this case, I stick with Django because:
  • Out of the half dozen languages I'm most comfortable with I still enjoy Python the most.
  • Out of the several Python web frameworks I have at least a passing knowledge of Django is the one I know best.
  • Since I'm not dealing with templates and forms, Django is really straightforward.
  • Opportunity cost. The time I spend becoming as familiar and comfortable with another framework is time wasted for no benefit.

Things pushing me towards a different framework:
  • django-rest-framework is a little hairy as soon as you stop doing very basic things like serializing a simple model with no relations. I'm really hoping DRF 3.0 clears up a lot of this. DRF could also use a version of http://ccbv.co.uk/ for it's own class hierarchy.
  • NEW BETTER FASTER HOTTER WEB 7.0
  • Maybe there's benefits to another framework that I won't learn until I become familiar with it.


xpander posted:

I'd decided that I was going to try this with my next project, which is interaction-heavy for its main functionality, and this makes me feel better about my decision. Do you have a fallback in case JS no workie? Or is that just not a concern in TYOOL 2014/2015?

gently caress 'em

(unless you've got metrics telling you they're a significant-enough portion of your audience. in that case you're the one who's hosed, because gently caress those guys blocking js)

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
I think it's actually possible to reconcile the idea of supporting nojs (especially for 3G where the js may take a while to come down the line) while still developing modern. That said, it involves bringing in Node to do essentially all your template rendering. If your primary back end framework has 'localhost' access to a Node server with a JSON API for spitting out template renders, potentially you can share all that code and give nojs something at least to look at on initial load.

This is especially possible with React, which is pretty much idempotent if used correctly.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Maluco Marinero posted:

I think it's actually possible to reconcile the idea of supporting nojs (especially for 3G where the js may take a while to come down the line) while still developing modern. That said, it involves bringing in Node to do essentially all your template rendering. If your primary back end framework has 'localhost' access to a Node server with a JSON API for spitting out template renders, potentially you can share all that code and give nojs something at least to look at on initial load.

This is especially possible with React, which is pretty much idempotent if used correctly.

Yeah, I actually started doing exactly this, but then realized that I just didn't care enough to go through the effort.

If your content should be search engine indexable, you've got to do some work on this as well...

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

No Wave posted:

I think you're definitely right. Do you still feel like you're getting enough out of Django, or have you been tempted to use a different framework?

Another thing Django has going for it is the huge community around it. There are a TON of things you can drop into Django and they 'just work' with minimal configuration.

Mulozon Empuri
Jan 23, 2006

fletcher posted:

Another thing Django has going for it is the huge community around it. There are a TON of things you can drop into Django and they 'just work' with minimal configuration.

What we need is a narrated version of that site.

epswing
Nov 4, 2003

Soiled Meat
Hey!  I'm having trouble reaching static content (css, js, etc).  My app is called 'website'.  In settings.py I have STATIC_URL = '/static/' which works in the development server, but not nginx. This is a stock digitalocean ubuntu 14.04 django 1.7.1 one-click setup.

I'm getting a 404 when I point my browser to blah.com/static/hello.css, with or without the following alias in /etc/nginx/sites-enabled/django:
pre:
    location /static {
        alias /home/django/django_project/website/static;
    }
Any suggetions?

Edit: That's odd. I looked in /var/log/nginx/error.log and I'm getting:
pre:
2014/12/04 16:33:15 [error] 1051#0: *242 open() 
"/home/django/django_project/django_project/static/css/hello.css" failed (2: No such file or directory)
That's supposed to be "/home/django/django_project/website/static/css/hello.css". What is that 2nd django_project doing there? :smithicide:

Edit 2: Despite restarting guni-- AH poo poo I needed to restart nginx too. Nevermind. :v:

epswing fucked around with this message at 23:22 on Dec 4, 2014

Hed
Mar 31, 2004

Fun Shoe
Can you paste your whole server block? There could be other matchings going on.
Can you set only the static location block on and put autoindex on and browse the directory?

Nginx normally runs as nobody but the files need to be readable by a set user (default www-data) in order for the web server to see them. I didn't think an error there was a 404 though.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
Hey django-rest-framework folks. For one of my serializers I'm returning a queryset of users but I want to restrict that to only non-staff users. Is there a way to filter the queryset that the serializer returns?

Adbot
ADBOT LOVES YOU

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Blinkz0rz posted:

Hey django-rest-framework folks. For one of my serializers I'm returning a queryset of users but I want to restrict that to only non-staff users. Is there a way to filter the queryset that the serializer returns?

Yup super easy to do, check out the get_queryset or queryset stuff on the Viewsets page.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply