Django: collectstatic does not collect my app - django

I am going to deploy on IIS so all the following commands were executed on Windows (Server2016)
This is the structure of my django project:
$ C:\inetpub\wwwroot\djangoProject
.
├── static
├── manage.py
├── websrv
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── urls.py
└── views.py
└── djangoProject
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.p
I set STATIC_ROOT in C:\inetpub\wwwroot\djangoProject\djangoProject\settings.py to:
STATIC_ROOT = 'C:/inetpub/wwwroot/djangoProject/static'
and obviously my app name (websrv) was added to the INSTALLED_APPS in settings.py too.
but when I run python manage.py collectstatic, the result is that only admin folder is collected under C:\inetpub\wwwroot\Alsatex\static and there is no folder for my main app (websrv)

Related

Django dev server not recognizing new files after directory modification

I have a Django project with 2 apps and was writing some functions in a utils.py file in one of the apps. I wanted to break this up into two separate files in their own subdirectory so I created a new directory 'utils' a level below the app directory and placed the two utils1.py and utils2.py files in there.
I had some issues with importing something from the other app so I ended up scrapping this idea and moving everything back into one file in the base directory of the original app, exactly like it was before. Now when I runserver it is not picking up any new files that are created within apps. Not just the ones that I recreated but any new files. Files that were created prior to the change are running just fine.
So, in summary new utils.py files that I recreated in the app directory are not running when the dev server is started, and when I try to run one of them manually they run like any other python file, but imports from other locations in the project are not being recognized.
No other changes were made and new files were running perfectly fine before the directory changes.
After the changes:
├── app1
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── permissions.py
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   ├── utils.py
│   └── views.py
├── manage.py
├── project
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── app2
├── __init__.py
├── admin.py
├── apps.py
├── utilities <--- added
├── util1.py
└── util2.py
├── migrations
├── models.py
├── serializers.py
├── tests.py
├── urls.py
└── views.py
After reverting back to previous structure (not working):
├── app1 <--- new files created here aren't running
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── permissions.py
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   ├── utils.py
│   └── views.py
├── manage.py
├── project
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── app2 <--- new files created here aren't running
├── __init__.py
├── admin.py
├── apps.py
├── util1.py <--- moved back into app directory
├── migrations
├── models.py
├── util2.py <--- moved back into app directory
├── serializers.py
├── tests.py
├── urls.py
└── views.py
I've tried clearing the pycache files, restarting the dev server, restarting terminal, etc. to no avail.
I figured out what was going on. My assumption was that any new python files in an installed app would be automatically run, but something from the file needs to be imported first from somewhere else in the project. Before the changes I had an import in the utils.py file so the dev server was running it, but after the changes there were no imports from elsewhere in the project. Issue is fixed and working now.

DJANGO 1.11 - Can't find fixtures

Problem
I was trying to use fixtures to populate my database, so I started by reading through the documentation for loaddata and by default, it looks in the fixtures directory inside each app for fixtures. My problem is that when I run python manage.py loaddata I'm getting an error, but when I give it the path for teams.yaml it works fine. Do I need to setup something for it to work?
Documentation
https://docs.djangoproject.com/en/1.11/howto/initial-data/#where-django-finds-fixture-files
Error
usage: manage.py loaddata [-h] [--version] [-v {0,1,2,3}]
[--settings SETTINGS] [--pythonpath PYTHONPATH]
[--traceback] [--no-color] [--database DATABASE]
[--app APP_LABEL] [--ignorenonexistent] [-e EXCLUDE]
fixture [fixture ...]
manage.py loaddata: error: No database fixture specified. Please provide the path of at least one fixture in the command line.
Team App Dir
team
├── admin.py
├── apps.py
├── fixtures
│   └── teams.yaml
├── __init__.py
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_auto_20190204_0438.py
│   ├── 0003_remove_team_team_name.py
│   ├── `enter code here`0004_team_team_name.py
│   ├── __init__.py
│   └── __pycache__
│   ├── 0001_initial.cpython-36.pyc
│   ├── 0002_auto_20190204_0438.cpython-36.pyc
│   ├── 0003_remove_team_team_name.cpython-36.pyc
│   ├── 0004_team_team_name.cpython-36.pyc
│   └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│   ├── admin.cpython-36.pyc
│   ├── apps.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   └── models.cpython-36.pyc
├── tests.py
└── views.py
Model
from django.db import models
class Team(models.Model):
team_name = models.CharField(max_length=32)
team_description = models.TextField(max_length=512, blank=False)
class Meta:
permissions = (
('create_team', 'Can create a team'),
)
Fixture (teams.yaml)
- model: team.Team
pk: 1
fields:
team_name: team_name_example
team_descrition: team_description_example
Should you define FIXTURE_DIRS in your settings file so django find your fixtures which there are there.
settings.py
FIXTURE_DIRS = [
os.path.join(BASE_DIR, 'fixtures')
]
# statements
Reference
The error says:
No database fixture specified. Please provide the path of at least one fixture in the command line.
You need to provide a fixturename in loaddata command, in your case:
python manage.py loaddata team
It was specified in the doc that by default Django will look into fixtures directory inside your app with the specified fixturename. Also the command accepts ./path/to/fixtures/ which overrides the searching of fixtures directory.

Create react app + Django deployment Uncaught SyntaxError: Unexpected token <

I've been trying to deploy an app to pythonanywhere but the page is just blank, because main.6f259c1b.js file throws error`
Uncaught SyntaxError: Unexpected token <
`
I've been following the instuctions on this article https://www.fusionbox.com/blog/detail/create-react-app-and-django/624/ and this https://www.techiediaries.com/create-react-app-django/
both articles suggest to create a view with following content
class FrontendAppView(View):
"""
Serves the compiled frontend entry point (only works if you have run `yarn
run build`).
"""
def get(self, request):
try:
with open(os.path.join(settings.REACT_APP_DIR, 'build', 'index.html')) as f:
return HttpResponse(f.read())
except FileNotFoundError:
logging.exception('Production build of app not found')
return HttpResponse(
"""
This URL is only used when you have built the production
version of the app. Visit http://localhost:3000/ instead, or
run `yarn run build` to test the production version.
""",
status=501,
)
and in app website/urls.py
urlpatterns = [
url(r'^', FrontendAppView.as_view())
]
Those instructions don't work for me. It's something that related to pushState routing, react-routing, I don't know. My app works ok in development server in localhost:3000, it only seen in pythonanywhere and local apache server with mod_wsgi.
This is my config of local apache(from Django documentation):
WSGIScriptAlias / /home/user/projects/myproject/myproject/wsgi.py
WSGIPythonHome /home/user/.virtualenvs/myproject
WSGIPythonPath home/user/projects/myproject/
<Directory /home/user/projects/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
This is root
DocumentRoot "/srv/http"
Part of my settings
REACT_APP_DIR = os.path.join(BASE_DIR, 'frontend')
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = [
os.path.join(REACT_APP_DIR, 'build', 'static')
]
All software are latest release.
Maybe this comment solves my problem, I just don't understand it. https://github.com/facebookincubator/create-react-app/issues/1812#issuecomment-286511320
This is my localhost screenshot
My project directory structure
├── api_v0
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── __pycache__
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── myproject
│   ├── __init__.py
│   ├── local.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── frontend
│   ├── build
│   ├── node_modules
│   ├── package.json
│   ├── package-lock.json
│   ├── public
│   ├── README.md
│   └── src
├── manage.py
├── requirements.txt
├── static
│   ├── admin
│   ├── css
│   ├── js
│   ├── media
│   └── rest_framework
└── website
├── admin.py
├── apps.py
├── __init__.py
├── management
├── middleware.py
├── migrations
├── models.py
├── __pycache__
├── tests.py
├── urls.py
└── views.py
Review the styles referenced on the page and make sure that all the CSS files are referenced on the page using a <link> tag and not a <script> tag.
reference
also there are nginx adubting , you can do it for request your site adminstator help

ShimmerCat - Use ShimmerCat as a reverse proxy for a Django app

I have tested ShimmerCat (https://www.shimmercat.com/) and sc-tool (https://pypi.python.org/pypi/sc-tool/) using this (https://www.shimmercat.com/en/info/articles/getting-started/). I am trying to deploy the application locally but a 404 error occurs.
This is the structure of my project:
├── db.sqlite3
├── devlove.yaml
├── manage.py
├── testapp
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── templates
│   │   └── home.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── testproject
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
└── static
│   ├── base.css
And this is my devlove.yaml file:
shimmercat-devlove:
domains:
www.test.com:
root-dir: testproject
consultant: 8080
cache-key: xxxxxxx
I haven't made any changes to settings.py
Do you have any suggestions on how to solve the 404 error? Thank you in advance.
With web-servers, you usually have your static files served by the webserver itself and the dynamic part of the application being proxied by the web server to your application.
So, a starting setup for your project would be:
shimmercat-devlove:
domains:
static.test.com:
root-dir: static
cache-key: xxxxxxx
www.test.com:
port: 8080
Everything indented under "static.test.com" would be served as static resources, therefore you need to modify your settings.py file to identify "static.test.com" as your static domain:
STATIC_URL="https://static.test.com/"
In addition to that, you would need to run your Django application in one of the usual ways, simultaneously with the server. For example, you could start by launching:
$ python manage.py runserver
and then in a new terminal invoke ShimmerCat's from the project's directory:
$ shimmercat devlove

What am I missing in Django production deployment?

I have the following codes for deploying a Django 1.9 project named deploy_project on a CentOS 7 server.
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "deploy_project.settings")
application = get_wsgi_application()
The above was generated by default.
httpd.conf
WSGIScriptAlias / /var/www/deploy_project/deploy_project/wsgi.py
WSGIPythonPath /var/www/deploy_project
<Directory /var/www/deploy_project/deploy_project>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
My project directory is in /var/www/deploy_project. Inside that I have an app called Deploy, the project settings folder called the same as the project name deploy_project and the manage.py file. I also have a db.sqlite3 file as I am not using MySQL, but my app just runs a view which shows Hello World. I am not using the database.
When I visit the server IP from browser, I receive a 404 Not Found page with the message The requested URL / was not found on this server..
tree output of project folder
├── db.sqlite3
├── deploy
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── 404.html
│   │   └── deploy
│   │   └── hello.html
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── deploy_project
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── manage.py
project urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('deploy.urls')),
]
app urls.py
urlpatterns = [
url(r'^$', views.Hello, name='hello'),
]
views.py
def Hello(request):
return render(request, "deploy/hello.html", {})
hello.html
Hello World!
Being the dumb developer that I am, I didn't restart apache service, so my settings didn't take effect.
For CentOS 7 my command was:
sudo systemctl restart httpd
That solved my problem. Thanks for your time #BurhanKhalid.