Django Advanced tutorial: How to write reusable apps - python

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.

Related

Django - How To Make Migrations For Reusable App

I generally use django as a full-blown web app, and am now attempting to make my first pull request on a reusable application. I normally create migrations like this:
python manage.py makemigrations
Of course, since this is a reusable application there is no manage.py file. Even if I add one, there is no settings.py file to point the manage.py to.
What is the accepted way to create migrations for a reusable django application?
The answer is that there IS a settings.py file in each application, I just overlooked it.

Saving Changes To Django with Nginx?

Anything I modify on my Django app has no effect with what's live on the webapp itself. I can delete the entire project directory and my Django app still works.
With Django and Apache, I would simply enter:
/etc/init.d/apache2 restart
And any changes made to my Django application would go live immediately after Apache was restarted. But with this new Nginx, the same thing doesn't work:
/etc/init.d/nginx restart
My Django home page still shows the:
It worked!
Congratulations on your first Django-powered page.
How do I make my Python changes go live with Django and nginx?
What is your Nginx configuration? What is your WSGI runner, are you using Nginx-wsgi or the inbuilt Django 'runserver' command?
It sounds like the "it worked" page is on a separate port, and you're testing it directly -- not via Nginx. It's really easy to get the two of them confused :)
Nvm just figured it out. For Django One Click Install Image on Digital Ocean you need to run:
service gunicorn restart
After you've made all the changes and you'd like to test changes to your web app.

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.

Running the Django REST framework quickstart tutorial

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 :)

App Engine problems using the command line

I am developing an App Engine project on Windows using Eclipse and Python 2.5 and Django 0.96. Everything in my app, including Django, is working nicely.
My problems arise when I try to use command line. For example, I'm told that if I want to clear the local datastore I can enter "python manage.py reset" but when I do so the response is "python: can't open file 'manage.py'".
I feel as if I have missed a configuration step. I have checked my system variables and "Path" includes "C:\Python25" (which I had added manually) but nothing Django or App Engine related. My .py extension is associated with C:\Python25\python.exe.
In my quest to solve this, and in trying to understand what manage.py is, I see that I might have had to create a Django project using "django-admin.py startproject [myproject]" but because everything works nicely from Eclipse I'm not sure if this is necessary now. In any case, if I try to enter this from the command line I get "'django-admin.py' is not recognized..."
Please, what am I missing?
If you're using Django 0.96 templates within a 'normal' App Engine app, then manage.py isn't involved at all.
/path/to/dev_appserver.py --clear_datastore .
is what you want, assuming you're CD'd to the root of your app.

Categories

Resources