Django :How to integrate Django Rest framework in an existing application? - django

How should I integrate Django-REST-API framework in an existing application or I have to create a new project?

You do not need to begin a new project. The basic steps are:
Install DRF, something like pip install djangorestframework
Add rest_framework to your INSTALLED_APPS
Define your serialisers, views and routes.
And that's it.
I suggest you follow the Quickstart and step through the Tutorial — it's pretty welcoming really.
I hope that helps.

I created a short note about how to do this with the photo gallery app tutorial from the Forcier et. al. book. I'm a Django noob, so please take this stuff as not-authoritative-perhaps-not-even-good. Here's the link to the post:
https://technote.fyi/blog/2018/01/26/how-to-add-rest-api-existing-django-project/
Basically, you create a new app, then in it write code for only three files: serializers.py, views.py, and urls.py. You don't write anything in models.py. Instead, you import the models from the existing app.
You then start off by creating serializers for all the models you want to expose, and then views for those serializers, and, finally, urls to call those views.

Related

should I use drf APIView's alongside with django Views in a production project?

I am just learning djangorestframework and my question is: "should I use APIView's alongside with Views?" my friend told me that I should have an api app and for some parts you should only use normal django Views and other parts related to admins the APView.
Now I'm a bit confused what should my project structure look like for exmple in an ecommerce project?
Thanks for your help!

create an admin's like application in django

Im really confused about what is all i need to consider for creating a django aplication with almost similar functionality to it's own admin.
The index page should deploy the list of models the user has access to modify or create...almost the same as when you put admin.site.register(MyModel) but with permission restriction. Im not sure how should i ckeck permissions, and show 1 ,2 or many "ModelAdmis" on my main page.
btw admin users are redirected to the admin index page, non-admins go to my page
Before you consider creating a django admin from scratch, you should read the answers to this question Django Admin app or roll my own?
I couldn't find any resource on how to create a django admin from scratch, but here's what you should do if this is your first time overriding a framework's functionality (in my humble opinion):
Understand and make sure you are comfortable with the django admin app
start from the docs https://docs.djangoproject.com/en/1.7/#the-admin
Head over to the django admin app source code so you can start reading the internals of the functionality you want to implement/override in your new admin app.
source code can be found here https://github.com/django/django/tree/master/django/contrib/admin
(this may involve reading other apps source code too)
After those two steps you should have an idea on how the admin app is implemented and it's dependencies, then you can start creating your custom admin app.
an example on how this may go can be found in this qestion:
How to override Django admin's views?
If you are building something new, try to separate the UI from the backend. You can build your UI using react, angular or whatever and interact with django using the API. To build the API you can use the Django Rest Framework.
Don't use the Django Admin as a public interface. Use that only for the admins!
If you start to use the Django Admin as interface for your public site, you'll fight with the package to tailor and secure the views to avoid destructive actions. What happen if you forget a readonly field? What if the user deleted something ON_CASCADE?
Building the UI you are totally free and you can customise easily everything without fighting the django admin package (it's awesome package but is not provided for public use)

Adding a blog to my Django website

I have created a website using Django 1.4.3. I'd now like to add a simple blog to it. I'm thinking a good way to do this would be to add a new blogging app to my existing Django project? Are there any simple Django blog apps that I could "drop in" to my existing project to achieve this?
Also, I have an existing blog on Posterous that I have exported (a set of folders and a wordpress_export_1.xml file.)
It would be great if I could somehow import this into my new Django based blog.
https://github.com/nathanborror/django-basic-apps
django-basic-apps (formerly django-basic-blog) has a blog app (and included apps to help said blog app) that might be what you're looking for.
Or you can try http://gettingstartedwithdjango.com/en/lessons/microblog-kitchen-sink/ . It is a free courses by Kenneth Love.

How to create views automatically from model as in Django admin?

I'm starting to use Django and I'm really impressed by the possibility of automatically creating views from a model.
So, I'd like to know if there is such functionality on other web frameworks. I know of RoR scaffolding, but that is not quite the same thing, since you need to change your views manually in case you change the related model.
For those who are looking for an answer here, perhaps django-baker (https://github.com/krisfields/django-baker.git) will do what you need.
pip install django-baker
Then add django_baker to INSTALLED_APPS and run
python manage.py bake your_app_name
Note that previously you need to remove urls.py and views.py from your app so they can be generated.
Then it will generate all the skeleton stuff.
https://docs.djangoproject.com/en/dev/topics/class-based-views/
I think I've found one possible solution. The concept is called model drive development. There are quite a few java frameworks that provide the ability to create a, rather simple, application directly from the model. This post presents some of them http://www.javaneverdie.com/java-frameworks/java-domain-driven-frameworks-review/

Easy-to-use django captcha or registration app with captcha?

I want to implement user registration using captcha in Django.
The workflow of django-registration app is a great, but it doesn't have captcha.
What captcha would you recommend to use with it?
Are there some other variants of registration+captcha or useful links on the topic?
This should work with Django-1.1 and don't be too hard to install.
django-registration is pretty extendable. One way to extend it is to provide a custom registration form. I'd recommend to use reCaptcha, e.g. with the widget and form field from here (archived). Then it is as simple as writing a custom form class and registration backend (which is simpler than it sounds):
from registration.backends.default import DefaultBackend
from registration.forms import RegistrationForm
class RecaptchaRegistrationForm(RegistrationForm)
recaptcha = ReCaptchaField(label="I'm a human")
class RecaptchaRegistrationBackend(DefaultBackend):
def get_form_class(self, request):
return RecaptchaRegistrationForm
The last step is to tell django-registration to use your backend. That step is described in the docs (I couldn't find a HTML version of the docs, sorry)
I've just had this problem, but the solution is dead simple.
I'm using django-registration, and I want a reCAPTCHA field for user registration. In just 1 minute:
download django-recaptcha (pip install django-recaptcha)
install it on your project. That is, copy the "captcha" folder to your project, add "captcha" to INSTALLED_APPS and add your RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY keys to settings.py too (as described in the installation instructions)
open registration/forms.py and add this field inside class RegistrationForm(forms.Form):
captcha = ReCaptchaField()
you will also have to import:
from captcha.fields import ReCaptchaField
And that's it. Less than a minute.
For those like me arriving late to the thread, there are a bunch of solutions out there now, which are pretty easy to install:
http://code.google.com/p/django-simple-captcha/
http://code.google.com/p/django-captcha/
https://github.com/inueni/django-captcha-field
https://github.com/justquick/django-math-captcha
https://github.com/marconi/django-mollom which uses the third-party Mollom service (which provides captcha and spam-filtering services).
I've successfully setup Django Mollom and Django Simple Captcha, and the hardest part was yak shaving around installing PIL on my Mac. Implementing the code was as straightforward as the docs for each would suggest.