Running the Django REST framework quickstart tutorial - python

I'm new to python and didn't use django before. I want to run the Django REST framework quickstart tutorial so I can use it for testing another application (http://django-rest-framework.org/tutorial/quickstart.html).
I ran in two issues:
1) I'm confused at the "Settings" step, I don't know what file is the listing suposed to be in. (I pasted the contents in urls.py, then I tried with a settings.py in the same folder as the other files.)
2) Just before the "Testing our API" section, I don't know the command to launch the project. (I tried "python urls.py" because url has references to the other files.)
Thank you.

Before you start using the django-rest-framework, you may want to learn more about django itself, so try the django tutorial.
https://docs.djangoproject.com/en/dev/intro/tutorial01/
About your question: when you start a project in django, it contains a settings.py.
Inside this file you have to edit the INSTALLED_APPS Tuple adding 'rest_framework,' in the end of it (one line before ")") and putting
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'PAGINATE_BY': 10
}
in the end of the file.
To launch the project you have to sync the database first, so do python manage.py syncdb, and once you did this call python manage.py runserver

settings.py obviously
python manage.py runserver
urls.py is an important file that manages the mapping of urls to the executables, but it's not an entry point to the django. manage.py provides tons of functionality and even able to run development server for you. In short, read the manual :)

Related

'Manage.py behave' not recognised as command in Django project

I've had to clone a repository involving a django project.
This is my first time using django and the project is configured a bit differently than what a normal django project would be. For instance it has a settings folder with a local.py file that contains a Local(Dev) class. I have installed behave_django, however when I run the command 'manage.py behave' it says that the command is not recognised.
Therefore I believe that whilst I have added INSTALLED_APPS = ('behave_django',) to the local.py file it is not getting recognised. I have tried adding it outside the class and within. Is there a way I can run a script to check my list of installed_apps, etc. Also I am new to python and the configuration side has gotten me a bit confused.
You need to provide the --settings option so Django knows you wish to use a different settings file with your task
manage.py behave --settings=myapp.local

Django Advanced tutorial: How to write reusable apps

According to Django Advanced tutorial: How to write reusable apps, "2. With luck, your Django project should now work correctly again. Run the server again to confirm this."
How to run the server? Having moved polls app from "mysite" to "django-polls" without manage.py and settings?
Please help with further instruction.
django-polls is the new reusable app, but mysite is still your site; the idea is that you can extract the polls app but mysite will still work. You run the server there.

Python social auth in Django, makemigrations detects no changes

I was following the documentation to get python social auth on my django project
https://python-social-auth.readthedocs.org/en/latest/configuration/django.html
And after adding 'social.apps.django_app.default', to the INSTALLED_APPS in my settings.py I run this:
python manage.py makemigrations
I get this
No changes detected
Shouldn't this command be doing something. Because without this I can't migrate to create the tables that are needed for the auth.
EDIT:
I've also tried this command, and still ended up getting the same result
python manage.py makemigrations main
where 'main' is the name of my app
Today i ran into this problem. The error is in the documentation itself.
You should run $ python manage.py migrate directly. It creates tables in the database.
All the old tutorials used makemigrations, I think it was used in earlier versions of django.
My answer will be cover some basics so one can understand this types of errors easily.
Let me clear some basic terminology about migrations in the latest versions of Django(From 1.7 to under development ones).
In older versions of Django when One has to make changes in Models (Eventually in Database) then One has to use South app to apply changes in Database without affecting older database.
Django developer community has included this south app in and after Django 1.7 and also provided some simple commands to apply migrations.
When one install New app(Above question scenario) or one make changes in existing models and wish to apply changes in database then one has to tell database about what changes they want to make. To do so one has to make migrations and below is the command.
$ python manage.py makemigrations app_name
or If it is initial then no need to specify app_name, it will consider all apps.
This command will generate migrations files which will include instructions for database to make what tables and what are the attributes of that table and what will be the relationships between tables and what are changes in the current tables etc etc.
Now one has to run below command to apply this all changes in database.
$ python manage.py migrate app_name
or If it is initial then no need to specify app_name.
Please shoot any questions If you have any and please take a look at Django's official documentation on migrations for more information.
The possible reason is your project doesn't use any database models of 'social' application yet. Add a URL in your urls.py, link it to 'social' urls.
to Django < 1.8
INSTALLED_APPS ['social.apps.Django_appConfig',]

How to automatically reload Django when files change?

How to automatically monitor .py, .js and other source code files to restart a Django (or any other for that matter) application and refresh the browser when the source changes? This is possible in Rails using guard, in JS apps using grunt-contrib-watch and the accompanying livereload browser plugin. How can I do it for Python web apps such as Django?
I start my Django server with
foreman start
this is my Procfile:
web: newrelic-admin run-program gunicorn app.wsgi
as suggested by the Heroku/Newrelic docs or the usual
python manage.py runserver
The runserver method does restart the server on .py source changes, but not the browser and doesn't watch other files - I could run guard alongside it, but then I have two processes I have to take care of, whereas grunt or rake offer unified interfaces. I'm wondering what is the recommended way of doing this among Python developers?
I could not find any detailed, comprehensive documentation on this - only incomplete discussions here and there.
You don't need a browser extension to accomplish auto refreshes. Take a look at https://github.com/tjwalch/django-livereload-server.
I posted a more extensive answer about this at https://stackoverflow.com/a/36961244/2950621
It works by using a manage.py command (server) to monitor your .js and other static files. The server sends a signal to the browser via websockets. There is some client-side code injected on every page. The injected code responds to the signal and refresh the browser.
Install this django app:
pip install django-livesync
On your django settings file add something like:
INSTALLED_APPS = (
'...',
'livesync',
'django.contrib.staticfiles',
'...',
)
MIDDLEWARE_CLASSES = (
'livesync.core.middleware.DjangoLiveSyncMiddleware',
)
Beware to register 'livesync' before 'django.contrib.staticfiles' if you are using it.
Now, just start your development server:
python manage.py runserver
Check this out for more details: https://github.com/fabiogibson/django-livesync
Using python manage.py runserver is what most use. You'll have to use another tool like: http://livejs.com/ to refresh the browser itself since Django really isn't aware of it.
Frustrated with all the explicit refreshes, I created a browser extension, for both Firefox and Chrome, to automate this. The extension works with a Django app that you add to your app list in INSTALLED_APPS. You can find out more at the github repo.
Though the repo has entire source code, the extensions are also available in the respective web store. Just search for 'Django Auto Refresh'. With these, you just need to copy the app into our project's folder and include it via INSTALLED_APPS. I wanted to add a pip setup script, but haven't found the time to do it.
HTH. Apologies if this sounds like self promotion.
I tried several answers here. But the browser did not seem to show the recent changes of the code. It worked for me when I opened Chrome in Incognito Mode.
If you are using Visual studio code, then you can just use python manage.py runserver and use VS code's auto save feature.
With this feature, whenever you'll make changes in your code, it will save everything and trigger a server reload.

Unable to execute Django runserver and no suggested solution seems to work

I've run Django servers on localhost before and have never run into this problem. I'm desperately trying to figure out what I've done wrong.
I'm using Django 1.4 with Python 2.7 on Ubuntu 12.04.
As far as I can tell I've configured everything correctly - I'm actually using another functional Django project I built as a go-by.
If I run the following command (or any recommended variation thereof) I receive an error.
django-admin.py runserver localhost:8000
Here is the error:
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
Can someone please enlighten me as to why this error is occurring, how to fix it and why it doesn't happen with my other Django project?!?
I've found many posts regarding this problem just by doing some quick Google searches, but none of the suggested solutions have helped - nor do I truly understand them.
I'm pretty sure you're supposed to run
manage.py runserver
from inside your project directory. It automatically loads your settings.py, etc.
From the Django docs:
Generally, when working on a single Django project, it’s easier to use manage.py. Use django-admin.py with DJANGO_SETTINGS_MODULE, or the --settings command line option, if you need to switch between multiple Django settings files
Providing some more code or examples of your directory structure might help.
First, the command is generally manage.py runserver 8000, so try that, and that make might a difference.
Second, in Django 1.4, the location of the settings.py file was moved. In previous versions of Django, the directory structure looked like this:
myproject/
settings.py
views.py
urls.py
myapp/
models.py
...
...
However, in Django 1.4, the main project settings and files were moved to a different directory:
myproject/
myproject/
settings.py
views.py
urls.py
myapp/
models.py
urls.py
...
...
So if you're using Django 1.4 but going off of previous examples, your settings.py might be in the wrong place. Additionally, I've found that when running django-admin.py startproject, it sometimes incorrectly creates two settings.py files, once in the old location and one in the new, which could be additionally confusing you. The only one that manage.py would pay attention to is the one in the project's directory.
If it turns out that your settings.py is in the wrong place but you don't want to move it, as your error suggests, you could set an environmental variable called DJANGO_SETTINGS_MODULE as the path to the Django settings.py you'd like to use for your project. I definitely don't recommend doing this.

Categories

Resources