Using Django templates in an iframe, 404 error - python

I have a Django site set up with the home page being home.html. Within home.html is a modal that is set to open automatically a few seconds after the home page loads. Within the modal is an iframe that should display template1.html. However when the modal comes up I have a 404 and I'm told the page didn't match the url patterns I defined. I'm not entirely sure where or how I go about getting template1.html to display in the iframe. The user isn't clicking on the main page to bring up a new template, which is how I'm familiar with using Django. Do I need to create another url pattern or view to achieve this or am I way off? Here are my files:
urls.py:
from django.conf.urls.defaults import patterns, url
from disease import views
urlpatterns = patterns('',
url(r'^$', views.home, name='disease_home'),
url(r'^disease/(?P<pk>\d+)/$', 'disease.views.disease', name='disease_disease'),
url(r'^treatment/(?P<pk>\d+)$', views.treatment, name='disease_treatment'),
home.html modal:
<div id="challengeModal" class="reveal-modal xlarge">
< iframe seamless scrolling="no" src="challenge1.html" name="challenge1"
id="challenge1"></iframe>
< /div>
views.py:
def home(request):
return render(request, "disease/home.html")`
Other views return the second template, template2.html, based on actions in template1.html
Thanks!

The short answer is Yes, you'll need both another url pattern AND a view for your iframe. An iframe is pretty much treated like an embedded browser window, it will make its own request to the server.

iframe src is just a url to a resource.
For example you could load your treatment page into it using
<iframe src="/treatment/1/"></iframe>
You will need to create a view to serve your challenge1.html template and an url to route to that view

Related

how to use google login link to my login page using django-allauth?

I have created a project which is working nicely.
I have a login page, signup page and profile page (as myspace). I just want to add an additional funtionality to my login page which is a user can login using google, fb, or instagram. but when i added the django-allauth to my project. it overwrite the existing login page.
I just want to add an icon of google login. and not the whole page how i can do so.
my login page looks like this earlier and at bottom i added a google icon in which i want to add the functionality of auth app
login pages is looking like this for now
Update:-
what i thought is how django-allauth works, that i just need to add a url in login page with <a href > tag with 'allauth/google' or something like this.
what i see is i need to add a new path in urls.py for allauth which automatically create a login, signup and profile page.
problem which i figure out is
I have an accounts_app app in my project. the url for accounts_app is same as allauth thats why it overwrite the template
url.py of camorid_project
urlpatterns = [
path('', include('camroid_app.urls')),
path('accounts_app/', include('accounts_app.urls')), -----i want to add allauth functionality to this url
path('accounts/', include('allauth.urls')), -------------i don't want to add this because it create an inbuilt templates
path('admin/', admin.site.urls),
]
what i want is i don't want to change login, signup and myspace page and i want to add icon in my login page with google login on click of which a user would be able login to the website.
in your login.html add {% load socialaccount %}
and then <a href="{% provider_login_url 'google' %}"</a>
this link will make you login with google api.
and you can style the button if you wnat..

django url doesn't redirect properly

I am trying to send user to next page using button like this
Weekly Report
Daily Report
but when I click on header or another button at daily URL keeps stacking like daily/weekly
instead /daily or /weekly. It doesn't clear URL and go to button instead just keeps adding it.
This is url.py
urlpatterns = [
path('', views.index, name="Home"),
path('weekly/', views.weekly, name="weekly"),
path('daily/', views.daily, name="daily"),
]
I tried different ways to call URL like {% url 'weekly' %} but still it's the same.
All urls only keep stacking. even home page button does keep stacking
You need to change to urls to either:
Weekly Report
Daily Report
Or use {% url %} tag in template:
Weekly Report
Daily Report

Getting 403 (Forbidden) when trying to send POST to set_language view in Django

I'm new to Django and am trying to create a small website where I click on a flag and the language changes. I'm using django i18n for that:
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
urlpatterns = [url(r'^i18n/', include('django.conf.urls.i18n'))]
urlpatterns += i18n_patterns(
url(r'^$', views.home, name='home'),
)
The problem is, when I run the following code:
templetatags.py
#register.simple_tag
def test():
r = requests.post('http://localhost:8000/i18n/setlang/', data = {'lang':'en', 'next' : '/'})
print r.status_code
home.html
<div id='country_flags'>
<a hreflang="en" href="{% test %}"><img id='en' src='{% static "mysyte/images/gb.png" %}'></a>
</div>
the result of r.status_code is 403.
What am I doing wrong?
Any POST request in django requires you to send the CSRF-Cookie by default. Your options around this:
Don't use a POST request. Use GET instead.
Send the CSRF-Token with the request. https://docs.djangoproject.com/en/1.9/ref/csrf/
use the decorator #csrf_exempt to remove any csrf-protection from a view (in your case the view for http://localhost:8000/i18n/setlang/)
don't send a request to your own app. use a link for the user to click on. probably your best option.
Note:
if you want to use the decorator w/ a class-based view, decorate the dispatch() function of that view

How to link html files together using Django 1.9?

I am currently programming in the atom code editor and python 3.4.0 as well as Django 1.9. I am new to django coding.
I have managed to link the html files and the css files, but I don't know how to link the html files together.
I have tried the <a href=" "> code because normally that works (I have previous knowledge in HTML) however, everytime I click on the link, it says webpage not found.
I am trying to link base.html file to the generalq.html file.
Does anyone know any tips?
You have to put an URL in the href of your <a> element that is handled by your urls.py and views.py files:
In blog/templates/blog/base.html:
My link
In compproject/urls.py:
from django.conf.urls import include, url
urlpatterns = [
url(r'^blog/', include('blog.urls')),
]
In blog/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^general/$', views.general, name='general'),
]
In blog/views.py:
from django.shortcuts import render
def general(request):
return render(request, 'blog/generalq.html', {})

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?

Categories