What is the best way to convert the project from django to django rest framework?
I have live django project, which is completely working, but now it want to convert the project into REST API.
I made the rest api for project which has new classes and function for the rest api with (form verification and same logic) copy from the django class which has been implemented.
So there is duplication of logic, so is there any way to do not replicate the logic and make running both of the classes django and django rest framework ?
Problem is when any update needed for any page then i have to code both side django and django rest framework , so there can be some mistake arise in copy.
One can create an API with Django using custom views but I advise one to use Django REST Framework (DRF) instead as it simplifies the process.
In short,
Install DRF with pip install djangorestframework (add it as well to the INSTALLED_APPS in your settings.py file).
Create a serializer per your needs (DRF has built-in serializers, like ModelSerializer). Note that OP can definitely use the models that OP already had in Django.
Create the views (DRF has generic views) and specify the URLs to access the views.
Let's say one has an app named books. In order to ensure the code isn't mixed between Django and the API, create an api folder inside of the books app and you should have something like this
books
-...
-api
--__init__.py
--serializers.py
--views.py
--urls.py
If OP wills, add reference to that urls.py in OPs main urls.py file,
urlpatterns = [
# ...
path('api/', include('books.api.urls', namespace='api')),
]
Related
I am trying to modify existing model(In Django CMS Blog application). There is a Post class, I can modify it in models.py that located inside the Django CMS Blog project, like so:
media = PlaceholderField("media", related_name="media")
post_title = PlaceholderField("post_title", related_name="post_title") # My code
content = PlaceholderField("post_content", related_name="post_content")
liveblog = PlaceholderField("live_blog", related_name="live_blog")
And after the migration the DB looks like this.
As you can see, the field is added. But how can I do that from my local project files? I don't want to add this code inside 3d party app models, because it will lead to problems with updating this 3d party app.
I think the best way is to extend the Post model from Django CMS Blog in your app.
my_app/models.py
from djangocms_blog.models import Post
class MyCustomPost(Post):
# This will have every field in the parent Post model, plus the ones you add below.
my_custom_field = ...
Then use MyCustomPost (in your views, forms, admin,...) instead of using the original Post model.
Didn't find any solutions except to copy that app to your project folder and alter it there.
I'm building a website using django-all auth for it's authentication and social authentication functions. The forms that come bundled with the app are hardly great to look at and hence I decided to create my own views.
The problem is: How do I create them while ensuring that the backend of Django all auth is still available to me? I've dug into the source code and found that it uses class based views for rendering and performing CRUD operations.
I want to know if I can subclass those views in my own app/views.py and just change their template_name field to my own templates. Any advice would be most helpful.
Thanks.
You can of course subclass the views, as long as you change your URLs to point to the overridden versions. However, there is no need to do this just to use your own templates; Django's template loader is specifically written with this use case in mind. Simply create your own directory inside your templates folder to match the one allauth is using, and create your own template files inside it; Django will find yours first and use them.
I have a django based CMS system I am working on and it uses tastypie. While all resources native to the CMS are registered in a separate file imported later into urls.py some of the clients using the system require loading their own custom apps. Is there some way to register an additional resource without changing the main codebase?
Turns out the clients in question override urls.py to include their apps. I replaced the API url pattern with a custom one and it seems to work.
I'm developing a web-app with common components and decided to pack those common components (some views, templates, logic) in a Django package.
Now the problem is: I want to access variables of my specific Django project, depending on the project, from the package. In this specific instance:
My Django package has an admin panel. A custom-made admin panel that allows me to manage a list of algorithms. However, these algorithms change. In one specific Django project I might have 3 algorithms, in another one I might have 5.
The admin panel in the Django package allows me to edit custom algorithm components, such as weights, test cases, etc.
Now my problem is, in the Django view, in the package, I want to access the Django project's algorithms in order to show them. As a list, for selection/editing/deleting, but also to view them in detail and edit them.
Now my problem is, obviously, I can't include something from the Django project in the Django package, otherwise it would become a dependency. And the algorithms are different and unique in each specific project.
So, in short, the question is how to access specific data of a Django project by a package used by that project, without making the package dependent of anything.
OK, so after some more investigation I found a possible solution for this. Will leave it here for informational purposes while waiting for an answer.
Instead of loading the package URLs as a module I use a custom function that passes the Django project's variables to the package URLs, and therefore, the views.
So in the project urls.py, instead of:
url(r'^package/$', include('package.urls'))
I use
url(r'^package/$', obtain_package_urls(custom_content))
where obtain_package_urls() is a function in the package:
def obtain_package_urls(custom_content):
urlpatterns = patterns('',
url(r'^url1$', view1, {'custom_content': custom_content},
url(r'^url2$', view2, {'custom_content': custom_content},
)
return (urlpatterns, None, None)
The goal is to, instead of the include function that includes the static URL-view mapping of Django, to use this function that returns a dynamic URL-view mapping with the custom content included in the views.
So in effective terms with the algorithms I ended up doing:
#Django project settings.py
obtain_algorithm_list():
return Algorithm.objects.all()
#Django project urls.py
from project.settings import obtain_algorithm_list
urlpatterns = ...
...
url(r'^package/$', obtain_package_urls(obtain_algorithm_list())
...
#In the package urls.py
urlpatterns = ...
url(r'^view1/$', view1, {'algorithms': algorithms},
...
#And then in each package view
def view(request, algorithms=[]):
...
use_for_something(algorithms)
So I have been following along with the Django Tutorial and have successfully created multiple "apps" that I now want to start integrating into a holistic website (which in Django seems to be called a project).
So here are my questions:
How do I create a site homepage that is mostly static data (HTML, CSS, and images), but also includes data from some of the models of my projects?
How do I link from this homepage to my apps? So if I have an app called "polls" (as in the demo), would linking to the polls page be as simple as linking to /polls?
I think the general approach is that you also have to add one app which glues all your other apps together. So if you need a special homepage which somehow has to have full or part access to all the other apps you create an app for it and point for example your root url to this app.
Following that (and depending if your other apps share data with each other or not) its really as simple as you said. The polls app could be accessible under /polls as an example, depending on how you configured it in your urlconf etc.