The following is the code that i got from a website for basic-authentication using "django-piston".
Here i have login.html page which i wana show,where do i put that and what is this realm keyword in the following code.................
from django.conf.urls.defaults import *
from piston.resource import Resource
from piston.authentication import HttpBasicAuthentication
from mysite.myapp.api.handlers import BlogpostHandler
auth = HttpBasicAuthentication(realm="Django Piston Example")
blogpost_handler = Resource(BlogpostHandler, authentication=auth)
urlpatterns = patterns('',
url(r'^blogpost/(?P<post_slug>[^/]+)/', blogpost_handler),
url(r'^blogposts/', blogpost_handler),
)
Django searches for template directories in a number of places, depending on your template-loader settings, but the most basic way of specifying template directories is by using the TEMPLATE_DIRS setting.
Look in your django-piston app where the templates are. Eg:
/path/to/site-packgages/django-piston/templates/template.html
If you have the default loader settings applied in your project, than you can override the templates by creating at template with the same path and filename but then in your project directory:
/path/to/projectdir/django-piston/templates/template.html
A realm is a community or territory over which a sovereign rules; a kingdom. 2. A field, sphere, or province: the realm of science.
Reading the Piston source code I think a realm is just a name that is passed to the authentication handler and probably only used by that handler to inform the user about the area he is about to receive access to: "Your about to receive access to [realm]".
Disclaimer: The realm guess is my best guess. I'm not a Piston user.
Related
I have added account app to installed_apps in my django project.
also I have added urls of account app as like below:
(r"^account/", include("account.urls"))
Its working fine.
Now I had to override SignupView class of account app. This is also working fine.
Now I have created a new class CreateUser(SignupView) and I want that only admin user will be able to create user. So I added a different url for CreateUser(SignupView) view.
Now I want that account/signup url with view SignupView will not be accessible anymore.
How can I block this particular url by keeping other urls active of account app as this is a library.
You can add a specific entry for the one URL that you wish to block before you include the packages urls.py. This will take precedence as Django loops over URLs in order looking for the first match
from django.views.generic.base import RedirectView
urlpatterns = [
path('account/signup', RedirectView.as_view(url='/')),
path('account', include('account.urls')),
]
I have a Flask-Admin project set up with Flask-Security as well. It is pretty much https://pythonhosted.org/Flask-Security/quickstart.html#id1 but just more advanced. I can access the login page at localhost/login and logout and localhost/logout. The logging in and logging out works.
The templates for Flask-Admin works and everything is displayed as I'd expect. However, there are no templates on my machine or docker container where the Flask-Admin app is run. I installed Flask by running pip install Flask-Admin. I know I can over ride the security log in by adding something like
SECURITY_LOGIN_USER_TEMPLATE = 'security/login_user.html'
to the config file and uploading to /templates/security/login_user.html. There is also using
{%extends base.html}
to have a common theme. Should I have template files already in my project?
Flask Security have a default login template, if you want to use your own template for login or register follow these steps:
Create in template folder the a subfolder named security
Add your html documents to this folder
Go to your flask configuration and add the following settings:
If your want the register functionality
SECURITY_REGISTERABLE = True
Add the name of your templates:
SECURITY_LOGIN_USER_TEMPLATE = 'security/login.html'
SECURITY_REGISTER_USER_TEMPLATE = 'security/register.html'
Remember to use the appropriate form in login.html and in register.html, usually causes doubts but is simple:
register.html: register_user_form.field
login.html: login_user_form.field
These are the configurations for this work correctly.
this repository can you to see and understand better doubt:
I am not even sure what category in Django this question falls in and I am very new to django. I have tried looking for Django post requests, parameter passing and even checked under Django APIs but have not found what I am looking for. What I am trying to do is create an API for my client but it must be done in Django. If I was to do this in .Net I could use http post and http get and web services but I am not at all sure how this is done in Django. What my client wants to do is to be able to see:
Enter username and password in url with parameters for date and id and be able to view rooms available based on what is entered
Enter username, password and dates and room code to be able to book the room
No interface needed just simple parameter passing through url. Is this possible with Django and if yes can somebody please point me in the right direction.
What you're looking for are captured parameters
Below is a code snippet from the above link.
# urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('blog.views',
url(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
)
# views.py
def year_archive(request, year, foo=None):
# view method definition
As of Django 1.10, patterns was removed from Django.
Here is a 2019 Django 2.2 solution.
It looks like re_path is the current recommended tool for capturing parameters via regular expressions:
https://docs.djangoproject.com/en/2.2/ref/urls/#re-path
# urls.py
from django.urls import re_path
from myapp import views
urlpatterns = [
re_path(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
]
# views.py
def year_archive(request, year, foo=None):
# view method definition
I have a Django CMS Project, which needs to create a Non CMS App "Achievemnets". The Customer wants full control over the page design, that means the page should be a CMS Page. However I have created specific views to show all the achievemtns in a page and clicking on the more link, it will show in detail. I need to port it to Django CMS I have tried as per the CMS App Hook method in the Django CMS Documentation. But none of them works.
Please tell me a tutorial which is good for learning CMS App Hooking
When you "hook" an application's URLs to a Django-CMS page, your app's URLs and view functions take over from there.
Let's say your Django-CMS page URL is: /achievements/
On this page, you want to display a list of achievements, which is going to come from your application.
#your_app.urls
from django.conf.urls.defaults import url, patterns
urlpatterns = patterns('your_app.views',
(r'^$', 'index'),
)
#your_app.views
from django.shortcuts import render
from your_app.models import Achievement
def index(request):
achievements = Achievement.objects.all()
return render(request, 'achievements/index.html',
{'achievements' : achievements})
The Django-CMS app hook you write tells Django-CMS which URLs to follow after in addition to the page you hook your app to. So, not only is Django-CMS going to pull content for the page by the slug, it's also going to hand off the matching URL pattern to your app.
I hope that makes sense.
I'm writing a web application in Django that is accessed from multiple domains to the same IP address. The idea is that each domain the application is accessed from will receive unique branding.
So for example, if there were two domains, reseller.com and oem.com, and you went to oem.com, it would take you to to the same website as reseller.com, but with differently themed content (say, sent from /static/oem.com/{files} instead of /static/reseller.com/{files}).
Basically my idea has been to define a custom template tag, that receives the SERVER_NAME as an argument, which would return the location of the content.
Are there any alternatives, or simply easier options?
Edit: I should probably add that I'm using MongoDB for this project, and as such it's more than likely Django's ORM won't be used for the project.
Edit again: More clarification; I'm using nginx.
Not sure how to do simple rewrite rule in nginx. Aside from a template tag (if you are only swapping out static content, then I think a template tag is the way to go), if the sites are gonna be different at all, template-wise, you can handle it by writing a custom template loader.
This allows you to pick what templates you would like to use when you render your page. This method has a graceful way of failing if the loader fails to find a matching template for your specific domain. If it doesn't find a match, it will fall back to your main templates directory. So you could have custom stuff for some domains, and more generic for others.
But to make a decision what to serve based upon a request header, you'll need to make the request available to the loader via _thread_locals, I do this in some middleware:
#custom.middleware.py
try:
from threading import local
except ImportError:
from django.utils._threading_local import local
_thread_locals = local()
def get_current_request():
return getattr(_thread_locals, 'request', None)
class RequestMiddleware():
def process_request(self, request):
_thread_locals.request = request
Next write a template loader (update the path to your middleware):
#custom.loaders.py
from os.path import join
from django.conf import settings
from django.template import TemplateDoesNotExist
from path.to.middleware import get_current_request
def load_template_source(template_name, template_dirs=None):
request = get_current_request()
host = request.get_host()
path_to_template_dir = None
for site in settings.SITE_TEMPLATE_FOLDERS:
if site[0] == host:
path_to_template_dir = site[1]
break
if path_to_template_dir:
try:
filepath = join(path_to_template_dir, template_name)
file = open(filepath)
try:
return (file.read(), filepath)
finally:
file.close()
except IOError:
pass
raise TemplateDoesNotExist(template_name)
and lastly update your settings file with three things 1) add the template loader (make sure its listed first) 2) add the middleware 3) and then add a new variable SITE_TEMPLATE_FOLDERS with a tuple of tuples containing domains and paths to template folders:
#settings.py
.....
TEMPLATE_LOADERS = (
'custom.loaders.load_template_source',
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'domain.middleware.SessionMiddleware',
'custom.middleware.RequestMiddleware',
)
SITE_TEMPLATE_FOLDERS = (
('mydomain.com', '/path/to/templates'),
('myotherdomain.com', '/path/to/other/templates')
)
...
Seems like a lot, but now you can easily add a new domain via your settings file.
You're looking for the "sites" framework.
For example, Apache has mod_rewrite that you can use to rewrite URLs:
RewriteCond %{HTTP_REFERER} ^www.domain1.com$ [NC]
RewriteRule /static/[^/]+ /static/domain1/$1 [L]
RewriteCond %{HTTP_REFERER} ^www.domain2.com$ [NC]
RewriteRule /static/[^/]+ /static/domain2/$1 [L]
(this is untested)
other servers also have similar functionality.
Just make sure your django application emits static urls that are site-independent and can be correctly rewritten.