I am facing a very strange problem. I have a Django project which runs fine in four environments (development, testing, staging and production), besides one strange bug.
In the production environment, the change form for a single model isn't rendered anymore. The rendered html page only contains the csrf token and then the submit buttons. All other change forms for other models are rendered just fine.
The result looks like this:
I am running the same code based (same commit and branch) on an identical staging server, and everything looks fine.
What could cause the faulty rendering of the form? How can I further debug it?
This is what I tried so far:
I switched back to a commit where I was absolutely sure that the admin change form worked. Form still not rendered.
I compared the pip packages between the Staging and Production. Both are identical (Django 1.7.1)
I checked the log files on the dev server. No Django error found.
I am running out of ideas how to debug this issue. Any suggestions?
Update March 23rd
It turns out that setting DEBUG=True, let's Django rendering the form. If DEBUG=True, the form rendering is failing silently. Why is that?
idea 1:DEBUG=False- static files are not handled by django but nginx /apache does
idea 2:Compare settings.py and url.py (mostly there r changes between staging/prod env's)
idea 3:For debugging run django stand-alone (runserver) without apache/fcgi
Related
I'm using gunicorn and nginx to serve a flask application, my website has a blogging feature where users can write blogs, once they do, their input is saved as an html file (I use tinyMCE to generate it) and a flask view is added to a views.py file. Now, in deployment, I just used
use_reloader=True
This ensured that every time a new file was added, it was detected, now in production, I don't know how to implement it, gunicorn has --reload option but the docs say that it's only for development. Can someone please provide an insight on how to implement this feature? Thanks!
You don't need a new view for every html file. You could use a generic view that renders the appropriate html according to the url requested.
I am reading through the following tutorial:
http://v1k45.com/blog/modern-django-part-1-setting-up-django-and-react/
I can't quite grasp what the added value is of using something like django-webpack-loader to fully integrate react.js and django when you can completely decouple django from react, running a separate frontend which links to a DRF rest api.
I may be comparing apples to oranges here, but I am not sure. Any help ?
From the django-webpack-loader tutorial:
Now that we’ve handed off the build process webpack, only thing we need on the django side is to know which bundle to include in our html pages. This is where django-webpack-loader comes in. It’ll also raise exceptions when webpack fails to build a bundle and will show some useful information to help debug the problem. During development, webpack loader will also block requests while a new bundle is being generated so that only the latest bundles are loaded.
This is the core functionality of that loader. It tells django which are the current bundles to be served. It does not server side render your react app nor does it replace an API that the app needs to consume.
When developing a react app you usually use some kind of bundler that transpiles your javascript code to es5, uglyfies and minifies it. So every time your code changes a new bundle will be created containing your new javascript. A lot of people are using webpack for this task.
Now when a request arrives in django it needs to serve the basic html along with the css and js to be loaded by the client that contains the react app. So django needs to know the path to the files to link. But the path may constantly change when webpack rebuilds them. django-webpack-loader helps you to keep track of these changes and includes the correct paths to the current bundles for your react app to be fetched.
It is not a replacement for an API. Its sole task is to resolve the correct paths to your react app files.
EDIT
So the primary purpose of django-webpack-loader is to ensure that the paths to the most recent build are resolved correctly and in turn can be served. What is the reason these paths will change ?
Weppack appends a random hash to a new bundle like bundle.4j2a032fg.js. This way django can tell the client to cache the script so that it does not have to be re-request it every time. The client will only request a new bundle if the url/path changed. But this also requires that django knows the newest bundle with the correct hash.
does server side rendering occur with the use of Django templates ?
Server Side Rendering (SSR) in the context of react means that a nodejs server actually runs the react application once on the server to produce the initial markup of the app. This can then be statically served with the app. It reduces the perceived loading time for the user because the initial state of the app will immediately be shown and it allows for search engines to statically analyse the page which is good for SEO.
While django does render templates it is only the static html around the react app which it will render by default. Django does not render the react app itself. The react app still needs to initially render itself on the client. So to enable django do do true server side rendering you would have to run a nodejs process next to django that does the static rendering of the react app for django to serve.
In the tutorial they are using templates. Templates are loaded in the backend. So user gets fully rendered page.
With django rest framework you need to write seperate fronend. More about this: https://nickjanetakis.com/blog/server-side-templates-vs-rest-api-and-javascript-front-end
I am using django.views.i18n.set_language() redirect view and HTML form where user can choose language.
I am doing everything as it's described in Django documentation for i18n translation .
The only difference that I made is that within HTML form I changed value of next parameter from {{redirect_to}} to {{request.get_full_path}}
Anyway, It worked completely fine while I was testing it locally. I could select different language and it would reload current page but with different language.
Now I put application on VPS where I use Gunicorn as application server and Nginx as web server. Now when I select different language it still changes it but it always redirect me to to home page / (site root).
I have no idea why is that happening now and how to change it. I want that he reloads the same page again instead of redirecting me to the / always. Anyway, at translation still works fine.
Thank you for your replys
Kind regards
Wander Nauta answered it in the comments
Are you sure request.get_full_path is available in the template?
You need to add django.core.context_processors.request in your template context processors settings, which is not there by default.
I've got a Django app running on localhost:80, and another app running on localhost:41984. Now, in the app, I'm trying to hook things up so that hitting localhost/view/41984 redirects to localhost:41984, without changing the URL in the browser: effectively, URL masking of sorts.
Could someone give me some pointers on achieving this? Django's HttpResponseRedirect does the redirection, but the URL changes too, which isn't what I want. I read somewhere that people do this with .htaccess, but I'm not planning on using Apache.
Thanks!
The best option should be, to run both app in the same server. Just add one to the INSTALLED_APPS list.
Then, you can do:
return redirect('some-view-name', foo='bar')
The view name is the one you define in the urls.py file:
url(r'^enter/$', 'yourApp.views.viewName', name='some-view-name'),
For more info https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect
If you need to run booth app in differents servers... I would say that you need a REAL web server, apache, ngix... what you want, but no ./manage.py runserver
I've been having trouble with my templates not updating in the Google App Engine Dev Python Server. If I leave the server running for a while, my templates will not update in the browser after I change them. My best guess is that jinja2 is caching these templates some how?
I'm rendering my templates using the following code:
_jinja_environment = jinja2.Environment(
loader = jinja2.FileSystemLoader(root_dir))
def write_template(self, template_name, template_data = {}):
template = _jinja_environment.get_template(template_name)
self.response.out.write(template.render(template_data))
I've tried restarting my app engine application, as well as undeploying and re-deploying, but the templates still don't update.
Anyone got any ideas?
Did you try any of the following
Clearing browser cache,
Renaming the template dir,
Changing the version
It none of them could fix it, then I dont know what is causing the problem.
This one is a similar question although the trouble happened after deploying.
Try the following:
Go to the admin console. On local machine it's: "http://localhost:8080/_ah/admin/datastore"
select the appropriate entity kind and then click on List Entities
click on Flush Memcache
Then try reloading the app.
I just experienced the same issue. I have one .py file and one .html file. I tried to edit a textarea to output some text in it, but it wasn't refreshed in the browser correctly. Still the old HTML file was displayed. After I restarted the app engine it worked just fine.
So I did it again and still the same problem. But if you edit the .py file and save it, the HTML file is reloaded into app engine. I don't know why, but I think deleting a character and undo it + save is faster than always restarting the whole app.
Maybe it helps!