Another issue with tiny_mce and django - python

I am a noob, and this isn't a problem actually. My tiny_mce works great on django development server, but doesn't work on production server. I think it has something to do with url configuration, because I get this error trying to fetch tiny_mce from source code of the page in browser:
Page not found (404)
Request Method: GET
Request URL: http://sav.2eng.ru/js/tiny_mce/tiny_mce.js
"templates/js/tiny_mce/tiny_mce.js" does not exist
I used this tutorial: http://vimeo.com/12903891
My url config is:
(r'^js/(?P<path>.*)$', 'django.views.static.serve', {'document_root' : 'templates/js'}),
and it works fine with 'manage.py runserver'.
admin.py has following code:
class AboutPageAdmin(admin.ModelAdmin):
inlines = [AboutPageImageInline,]
list_display = ('p',)
class Media:
js = ('/js/tiny_mce/tiny_mce.js', '/js/textareas.js')
What am I supposed to do with this url?
Thanks in advance.

Problem solved by adding abslolute, not relative path. I'll never forget this. Hope this would be usefull for someone.

Related

DJANGO: Set site_id to sites pk

I am following this tutorial for email verification.The version the author is working with is old.I got an error that said
Reverse for ‘activate’ with keyword arguments ‘{‘uidb64’: b’OA’, ‘token’: ‘4tm-3fcfb375c8ba14f9a95b’} . I got that fixed through the first comment . The email got sent .But the link led to www.example.com . The second comment tells how to fix that . The comment was :
For those are using Django 3, You should change some code
Six is deprecated in Django 3, you can use ‘import six’ instead of ‘from django.utils import six’
To send html email, add
email.content_subtype = “html”
after EmailMessage Object.
activate url should be
path(‘activate//’, views.activate, name=’activate’),
get_current_site(request) will return example.com as default when SITE_ID=1 in your settings.py. Add your site name and domail in admin site (/admin/sites/site/) and replace SITE_ID with your sites pk.
But I did not understand how to set SITE_ID to my sites pk.
In settings.py set
BASE_URL = 'https://www.yourdomainname.com'
Don't include trailing /
Also, you have to mention this line in the same file
SITE_ID = 1
Moreover, go to your django admin pannel , default is /admin, and go to the sites tab/model and add/edit your site according to your ip/domainName and display name . This is the important part to do.
If you are testing locally for now then you can use 127.0.0.1:8000 in your ip/domainName and yourdomainNAme.com in display name
If you are deploying or using live then you can use actual ip of your server into ip/domainname or also you can use your domain name (as it's mapped to your ip via DNS system) and display name will be the same.
Hope, it will resolve your problem or issue.

Changing Site Name in Django 1.9

I am currently following a tutorial which introduces a custom Bootstrap 3 template and builds a Django site using it. In the tutorial they suggest that one changes the following template snippet:
<a class="navbar-brand" href="index.html">Start Bootstrap</a>
to the following snippet.
<a class="navbar-brand" href="/">{{ request.site.name }}</a>
However, when I make this change, no site name shows. I am wondering where I should be setting this name. If it helps, I am using Django CMS and there is only one site called example.compopulated in the Sites section of the Administration.
Had the same problem, found this in the documentation.
If you often use this pattern:
from django.contrib.sites.models import Site
def my_view(request):
site = Site.objects.get_current()
... there is simple way to avoid repetitions. Add django.contrib.sites.middleware.CurrentSiteMiddleware to MIDDLEWARE_CLASSES. The middleware sets the site attribute on every request object, so you can use request.site to get the current site.
You can add site name or change existing site called example.com in the Sites section of the Administration.
To access Site object you need to provide SITE_ID in your settings.py
If you are changing example.com then use SITE_ID = 1 in settings
To render site name in django templates, get value from site model using
from django.contrib.sites.models import Site
current_domain = Site.objects.get_current().domain
then pass current_domain to template
It seems that you have to enable the sites framework, refer to doc.
first yo have to understand that in the setting you have a site_id set to 1 and in database migrations an example site with domain name example.com is created. So, you have two options:
You can change this default from django site models like this: python manage.py shell -c "from django.contrib.sites.models import Site; Site.objects.filter(domain='example.com').update(name='My Site', domain='mysite.tld')" then you restart the server.
You can insert a new row in the site models like this: python manage.py shell -c "from django.contrib.sites.models import Site; mysite,_=Site.objects.get_or_create(id=101, name='mysite.tld', domain='mysite.tld'); print(mysite.id, mysite.name)" Then change the site_id in the settings to the new site_id which should be 2 since it is the second new row.
You can take a look at this Github trend for more clearity.

url patterns for home views or using views accross apps

I've been reading about urls, but I can't seem to find a analogue to what I'm trying to do. I have an app called profile, the views.py in this app queries the database and returns user specific content - I use filters to summarize the db content to whatever user is logged in. In my "home page" I would like to have a summary of the database across all users.
so the urls used in the profile app looks like this:
url(r'^profile/$', 'profile.views.profile', name='profile'),
url(r'^profile/usrDash$', 'profile.views.usrDash')
the first one renders the "profile page" and the second is used by an ajax call to send some user specific info which in turn is used to formulate the query. That's working fine; no issue there.
so what if I wanted to display the same information, use the same query, on the "home page" too? How do I do that? Not exactly what I want to do, but If I can this to work I can adapt it later. I've tried:
url(r'^home/usrDash$', 'profile.views.usrDash')
but ajax doesn't seem to like it. there are no error messages it just doesn't POST anything.
I've also tried writing another view in home.views.py, but I can't seem to get the url right. Since the url pattern for "home page" is:
url(r'^$', 'home.views.home', name='home')
wouldn't the url for a query at home.views.py be:
url(r'^/usrDash$', 'home.views.usrDash')
The ajax call in question looks like this:
$.ajax({
method: "POST",
url: "profile/usrDash",
dataType: "json",
data: {
csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
someVariable: someVariable,
},
success: function(Data) {
...
...
},
error: function() {
...
}
});
So the ajax call from "home" is a copy of the ajax call used in "profile" with the modification of adding the profile/. But that doesn't work either.
Thanks in advance for your help.
Regards.
It looks like you have two apps, one called "profile" and another called "home". If I understand correctly, you would to like make an ajax post request processed by "profile.views.usrDash" to get information for the home page.
So, why not just make the ajax request to '/profile/usrDash'? Why are you associating a different URL pattern with the usrDash view?
The code you provided which attempts to reuse the usrDash view has an incorrect path. It should be profile.views.usrDash
Turns out that there were two issues with my code. The first was the way the csrf token was being passed to ajax. In my profile app this works:
csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
But it doesn't work in home app. I don't know why. It works in one app and not the other. What did work is:
csrfmiddlewaretoken: '{{ csrf_token }}',
I found this answer on this post:
csrf token issue with multiple templates
The second issue was the url, which was easy to diagnose once the csrf token issue was resolved. the final url is simply:
url(r'^usrDash$', 'profile.views.usrDash')
No need for the leading /.
Thanks to ArbyA for taking time to look through my code and thanks to doniyor whose response to the post linked above helped finally get this working.
Regards.

Django url with prefix - not working correctly

I am using Django 1.7 with Mezzanine.
URL of my pages has a prefix www.example.com/example
So I use:
FORCE_SCRIPT_NAME = '/example'
It works for default pages like blog. Blog has set url blog and it goes to /example/blog. But if I create custom link (for example in admin), it does not work. It skip /example in URL and goes directly to /.
How to fix that?
Did you wrote the pattern in urls.py?
something like this:
urlpatterns = patterns('',
url(r"^example/$",HandlingClass.as_view(),name='example'),)
Finally I found a solution.
I added FORCE_SCRIPT_NAME into TEMPLATE_ACCESSIBLE_SETTINGS in settings.py. So it is look like that now:
TEMPLATE_ACCESSIBLE_SETTINGS = ('FORCE_SCRIPT_NAME', 'ACCOUNTS_APPROVAL_REQUIRED', 'ACCOUNTS_VERIFICATION_REQUIRED', 'ADMIN_MENU_COLLAPSED', 'BITLY_ACCESS_TOKEN', 'BLOG_USE_FEATURED_IMAGE', 'COMMENTS_DISQUS_SHORTNAME', 'COMMENTS_NUM_LATEST', 'COMMENTS_DISQUS_API_PUBLIC_KEY', 'COMMENTS_DISQUS_API_SECRET_KEY', 'COMMENTS_USE_RATINGS', 'DEV_SERVER', 'FORMS_USE_HTML5', 'GRAPPELLI_INSTALLED', 'GOOGLE_ANALYTICS_ID', 'JQUERY_FILENAME', 'LOGIN_URL', 'LOGOUT_URL', 'SITE_TITLE', 'SITE_TAGLINE', 'USE_L10N')
Now is possible to extend the urls in patterns easily:
{{ settings.FORCE_SCRIPT_NAME }}/rest/of/url
Everything works now.

How to change the url using django process_request .

This is my code :
class MobileMiddleware(object):
def process_request(self, request):
if request.path.startswith('/core/mypage/'):
request.path='/core/mypage/?key=value'
print request.path,'aaaa'
I want to add a param key when the page url is /core/mypage/,
and the url of the web browser would be changed to http:www.ss.com/core/mypage/?key=value
However, the url in the browser is not changed.
What can I do?
For googlers - I tested with request.path_info. If you want to change URL in middlware, change request.path_info in process_request.
request.path_info = <change request.path_info>
Please Note that I do not suggest or forbid to use this. I'm just saying if you want to change urls, this is the way you can.
The problem is that HttpRequest.path is a plain attribute. Changing it does not make any new instructions for the browser. You're probably looking for the redirect method which will actually force the browser to go somewhere else.
Try This
return HttpResponseRedirect('/core/mypage/?key=value')
The request.path_info did not change the url in the browser address bar for me but this redirect did:
from django.shortcuts import redirect
class DomainRedirectMiddleware(object):
def process_request(self, request):
if request.path.startswith('/core/mypage/') and not request.GET:
return redirect('/core/mypage/?key=value') # works!
#request.path_info = '/core/mypage/?key=value' # works, but does not change url in browser address bar
Django also provides a "Redirects App" since Django 1.3, which includes the following middleware: 'django.contrib.redirects.middleware.RedirectFallbackMiddleware' . See the redirects app documentation, it lets you create redirects from the admin interface.
I tried the same redirect using the app and it worked. Cheers!
I haven't tested this, but try something like request.GET["key"] = val
Edit: or maybe use request.path_info instead of request.path

Categories