How do you actually use a reusable django app in a project?
Asked Answered
C

2

18

This question has been troubling me for some days now and I've tried asking in many places for advice, but it seems that nobody can answer it clearly or even provide a reference to an answer.

I've also tried searching for tutorials, but I just cannot find any type of tutorial that explains how you would use a reusable third-party django app (most tutorials explain how to write them, none explain how to use them).

Also, I've taken a look here:

How to re-use a reusable app in Django - it doesn't explain how to actually use it IN a project itself

and here:

How to bind multiple reusable Django apps together? - the answer by aquaplanet kind of makes sense, but I thought I would ask this question to solve the mental block I am facing in trying to understand this.


In order to best explain this, let me do so by example (note, it is not something I am actually building).

I am creating a project that acts like Reddit. I will have users, links and voting/points. Based on this crude example, I will want to reuse 3 (arbitrary) third-party apps: user, voting/points and links.

I decide to use each of them as any other python package (meaning that they will be treated as a package and none of their code should be touched) [would this method actually work? Or do you have to be able to edit third-party apps to build a project??)

With these apps now within my project, I will use a main app to handle all the template stuff (meaning everything I see on the frontend will be in a single app).

I will then either use that same main app for custom logic (in views.py) or I will break up that logic among different apps (but will still use a single frontend-only app).

From the 3 paragraphs above, is this structure applicable (or can it work) ?


Now lets say that this structure is applicable and I am using a single main app for the frontend and custom logic.

What would I write in models.py? How would I integrate things from the 3 reusable apps into the main models.py file?

How would I reference the reusable apps in views.py? Lets take the example of contrib.auth

With this built-in app, for logging out I would write:

from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect

@login_required
def user_logout(request):
    logout(request)
    return redirect('/home/')

Although the above code is simple, is that basically how things would be done with any reusable app?

My question is very long, but I think that this reusable app issue is something a lot of developers aren't quite clear about themselves and maybe this answer will help a lot of others who have heard about the promises of reusable apps, but fail to understand how to actually use them.

Colugo answered 16/2, 2014 at 13:36 Comment(2)
Did you manage to do it? would you like to share with us plz..i'm in the same situationApophasis
I recommend you try using it like you would use a function. Eg. If you are using function X(var1, var2), you import the module/library in your mainapp.py and you then use X(var1, var2) by basically passing the variables you want to pass and letting the function do what it does (and likely returns something). I think I eventually saw it from a point of abstraction, where the code is "written somewhere else" and you are using functions, etc. from that "somewhere else" in your main file without rewriting all of the same functionality again.Colugo
R
8

TL;DR:

Nope & it depends...

Some (Very) Common Reusable Apps

... those are all reusable Django apps, that happen to be shipped with Django (most of them were not, at some point in time)

Ok, some other reusable apps that don't ship with Django:

Those are all truly reusable apps, and nothing less. There are very many more apps like that.

How do they do it?

To me your question looks more like "how do I build reusable apps", then "how to use them". Actually using them is very different from app to app, because they do very different things. There is only one rule: RTFM No way around that either.

Often, they rely on one or more of the following:

  • additional value(s) in settings.py
  • addition (usually one include statement) to urls.py
  • subclassing and/or mixins for Models, Forms, Fields, Views etc.
  • template tags and/or filters
  • management commands
  • ...

Those are all powerful ways though which your app can provide functionality to other apps. There is no recipe (AFAIK) to make a reusable app, because there are so many different scenarios to consider. It all depends on what exactly your app should do.

Reusable apps provide functionalities

I'd argue that it's important to not think of reusable apps as "working together" with other app, but instead recognize that that they "provide functionality." The details of the functionality provided should dictate the way the target developer is supposed to use your library.

Not everything should be reusable

Obviously enough, even though many apps can "in principle" be reusable, it often makes little sense to do so, because it is way faster to clump things together (and make them just "work together").

Reinke answered 16/2, 2014 at 14:9 Comment(2)
@Colugo I sure didn't mean to be rude. In fact, I'm re-reading the answer and can't find what you might consider to be rude, other then the well-established RTFM acronym, that I used to affirm a point (the only way to make use of a third party reusable app), and not to offend anyone. But if I did offend you, I am sorry for it. Yes, I didn't respond in detail to your particular question, because I thought your view of reusable apps might benefit from taking a step back and looking the question from what seems to me like a bit different angle.Reinke
Perhaps the only way to figure it out is by trying and trying until it works. Which is what I will do. About reading the docs, they lack the info about this, but it doesn't matter. As per your apology, thank you for that and all is good. I just felt that RTFM wasn't appropriate, as I don't ask questions on SO without searching first.Colugo
G
1

I'm not sure why you think you need a main app for the "frontend" stuff. The point of a reusable app is that it takes care of everything, you just add (usually) a single URL to include the urls.py of the app, plus your own templates and styling as required.

And you certainly don't need to wrap the app's views in your own views, unless you specifically want to override some functionality.

I don't understand at all your question about models. There's no such thing as a "main" models file, and using a reusable app's models is just the same as using models from any of your own apps.

Normally you would not edit a third-party app, that would make it very hard to integrate updates. Just install the app in your virtualenv (you are using virtualenv, of course!) with pip, which will put it in the lib directory, and you can reference it just like any other app. Make sure you add it to INSTALLED_APPS.

Gauntlet answered 16/2, 2014 at 14:8 Comment(1)
"I'm not sure why you think you need a main app for the "frontend" stuff." . The reason why I thought this might work was because of situations where you want a single frontend to interact with multiple apps on a single page. Like say you are creating a home page for a news portal, you may have a main content area and a voting poll on the side. 2 different apps in 1 page. That is why I thought a single frontend could handle it all.Colugo

© 2022 - 2024 — McMap. All rights reserved.