Having html pages and a flask application on the server - python

I have a situation where i have a lot of html files and a flask application on other side which will be running on the server.
I want to configure server in such a way that html file is displayed when i go to www.example.com and flask application is called to render pages www.example.com/app
Here i have huge data to display on the website and the other flask app is used for customer specific.
I cannot have #app.route for thousands of html pages to render that particular page. So now i want to configure my web server where html files are accessed when i go to my website and it says away from the flask application and flask application is called when i go to app.
Thanks

Related

Conditional flask Login login_view

I have a flask web server, and it uses flask_login for user authentication.
My web server servers both websites and native mobile applications.
I want end-points which serve website (accessed by browsers) to serve login_view when user is not logged-in.
But the end-points which server native applications (android and ios) to respond with HTTP 401 response code.
How can I set LoginManager.login_view conditionally only on few endpoints

Django-allauth - URL isn't included in the app's domains

I'm trying to set Facebook login and registration on my web page (localhost for now).
Installed allauth
Set settings.py
Created an facebook app
And now, when I click on http://127.0.0.1:8000/accounts/login/ facebook href, it returns this alert:
Can't Load URL: The domain of this URL isn't included in the app's
domains. To be able to load this URL, add all domains and subdomains
of your app to the App Domains field in your app settings.
According to sites, I've set domain name (in Django-admin Sites) to
localhost
What should I do to make it work?

Flask: Turn on DEBUG only for web views blueprint

In a flask app I'm developing, I have some web views, which return a normal web page content and an API blueprint. For development configuration, I turn on debug, which is handy to see the backtrace in a browser:
DEBUG = True
However, this also applies to the API blueprint. Dumping HTML content into the console when doing a request through httpie is not very nice. Is there a way to have debug only for the web views and not for the APIs?

Redirect example.com users to example.com/#/login

I am working on a project that uses Django and Angular. I do not have a background as a web developer so please try to explain your answer so that a novice person can understand it.
Basically I want to make it so that the login page is the default page instead of the index page.
I currently have the following url handler in my main Django project urls.py:
url(r'^$', 'core.views.generic.index')
I also have another urls.py in an app called core that sends visitors to the login page:
url(r'^/login$', private.MeLogin.as_view())
Now I want the login page to become the default page instead if the index page. How can I do that?
I have tried adding the following the the views file in the core app:
#login_required(redirect_field_name='', login_url='#/login')
def index(request):
return render_to_response('html/index.html', locals(), context_instance=RequestContext(request))
Unfortunately I get the message
This webpage has a redirect loop
I do not know how to solve this problem. Basically I want users to be redirected to the login page if they enter any URL that is not handled by other URL handlers. When they successfully log in they are redirected to a dashboard page automatically.
EDIT:
The login page URL is handled in the core urls.py file and points to a different view than index.
url(r'^/login$', private.MeLogin.as_view())
Web servers, in this case Django, do not see the fragment after the #. You are redirecting from / to /, creating a redirect loop.
If you want to redirect to the Django login url, you need login_url='/login'.
As an aside, you should remove the leading slash from your regex r'^/login$.
In #login_required(redirect_field_name='', login_url='#/login')
remove redirect_field_name='', it really is not neccessary and make sure that #/login in login_url='#/login is the same as in your url.py file:
like
views.py
#login_required(redirect_field_name='', login_url='accounts/login/')
as
url.py
url(r'^accounts/login/', auth_views.login),
I assume your Angular & Django apps are running in two seperate ports, like:
Django on port 8000
Angular on port 1000
So if you give /#/login it will redirect for the same with Django port (8000/#/login).
So why don't you give the full URL www.example.com/#/login?

Flask building urls for api and view

I am writing a flask application with angularjs frontend. Templates aren't rendered from flask yet, they are delivered as static files. My api endpoint is as following:
#route('/projects', method=['GET'])
def rest_projects(self):
"""TODO: CRUD operation
"""
Project = Model.get('project.project')
return jsonify([project.serialize() for project in Project.search([])])
url_for works perfect with above endpoint but is there any way it can build urls for js templates also?. Say if url has api prefixed ie: /api/tasks, it comes to this handler otherwise deliver template. Right now I am using nginx to achieve that but is there anything wrong in my design which is restricting me to do this from flask only.
Flask is not intended to serve static files and you are better off keeping your files served by nginx for performance reasons. You can serve static files however using send_static_file() or send_from_directory methods.
http://flask.pocoo.org/docs/0.10/api/

Categories