Prevent Django from prepending domain to beginning of URLs in RSS feeds - django

I'm creating an RSS feed that contains application links for the URLs, but Django is prepending the domain name to any link that doesn't begin with http. So, URLs that should be itunes:// are ending up as http://example.comitunes://...
Update:
Because I'm not building the feed based on a model, I'm using the following in the Feed class to create the links. I've verified that they are exactly as they should be when passed to item_link(), but Django appends http://example.com to the front of every link that doesn't begin with http:
def item_link(self, item):
return item['url']
if link is http://foo.com/some_external_domain/, there's no problem.
if link is itunes://some_itunes_store_link, Django turns that into http://example.comitunes://some_itunes_store_link
The domain name is correct. That's not the problem. The problem is that I can't have the domain name prepended to an application link without breaking that link. I can't figure out how to tell Django to stop correcting links that don't need to be corrected.

This isn't possible in Django.
According to Django's feed documentation:
If link doesn’t return the domain, the syndication framework will
insert the domain of the current site, according to your SITE_ID
setting.
My solution was to just build the entire RSS feed manually as a template. It's an unfortunate solution for what should be a simple option in Django feeds, but it works.

Related

django redirect to subdomain with exact same url

I have a Django website accessible at codewithbishal.com. This is basically a django blog website and all the blog articles are accessible at codewithbishal.com/example/<path>
But now I want to completely restructure the website{such that it is accessible at blog.codewithbishal.com/<path>} and I do no want to lose the current SEO therefore I want to configure django redirect such that when someone launches codewithbishal.com/example/<path> through google search search result or directly through link, they are redirected to blog.codewithbishal.com/<path> instead of a 404 page and eventually google will start showing the new links instead of old once.
Update: Should I configure redirect through apache .htaccess?
You can use the Django redirects app. Just click the link and find the guide.

Reversing urls in Django / Celery

I have been asked to send an email on friday with a list of projects matching a set of conditions. The database front end is written in Django and its an in house application. I would like to link to the admin page of the project in the email.
On my development machine I am trying
reverse("admin:index")
and I notice that it is only returning
admin
from the Celery task, whereas in standard Django views this would return
127.0.0.1:8000/admin/
Is there a way around this? I would prefer not to hard code the start of the urls.
I have set up Celery as described in the "first steps with Django" tutorial, and have a
myproject/myproject/celery.py
file defining the app (alongside settings.py and urls.py) and
project/myapp/tasks.py
file with the actual tasks.
reverse() always returns a domain-relative url, i.e. /admin/. One option to increase portability is to use the sites framework.
As you don't have a request object in your Celery task, you have to explicitly set the SITE_ID in your settings and set the site's name and domain in your database. Then you can do:
from django.contrib.sites.models import Site
url = "%s%s" % (Site.objects.get_current().domain, reverse('admin:index'))
Usually URLs in django are relative - i.e. the "127.0.0.1:8000" part you see when you hover the link comes from the fact that you are browsing the site from that location. Try looking at the href value of the URLs generated.
A simple solution to your problem is to concatenate the reverse output with the domain, i.e.
'http://example.com/'+reverse('admin:index')

SIteCore - multisite - displaying page from wrong site

We have a multisite SiteCore setup with 2 sites within the same .Net solution.
This works by setting the rootPath property on a Site Definition in web.config to limit the site to part of the SiteCore folder structure.
This works well apart from when pages are created with the same name as in the other site then it's serving content from the other site! We have inherited a fair bit of custom code in this solution form the other site so this may be the cause but dont know what Im looking for ...
Thanks
How are you referencing the sites? Do they each have their own host name? Do you have the "hostName" property set for the site node in the Site Defintion?
I will assume that you are not referring to them this way and instead, the sites are using the "virtualFolder" property. If both sites have the same default value of "/" for virtualFolder, attempting to get to either site will result in Sitecore rendering the first site that it matches on, which would be the site listed first.
Try putting the actual site name for "virtualFolder" and "physicalFolder" (e.g. "Site1" and "Site2", respectively). Then you can address your sites as http://yourserver.com/Site1 and http://yourserver.com/Site2. The "virtualFolder" will match first and render the correct site.
See Configuring Sites in the web.config File on SDN for additional information.
Hope this helps.
It turns out this is happening in this case because of a System alias that is redirecting for a subset of pages

Referrer Spoofing in Django

I have been searching internet for some good docs/articles on how to implement referrer spoofing using django or django templates.
The project that we are working right now is a dynamic website, where in we get results from mysql database based on the search done by the user.
I am not getting any idea of how to implement this or know of any packages to do this.
Example:
If the original url in mysql database is http://www.google.com and my website is www.example.com.
The link that I should show is www.example.com/redirect/dfjldfjlsdf which will redirect to google.
Note: I am not posting any code/or what I tried because I am not getting any ideas on how to implement this.
Please help me.
I wrote a redirection script. The steps are:
Setup some url handler.
Setup a view for this handler.
Setup a meta refresh html template.
Metarefresh template will blank the referrer before sending the end user to the target url.
If you want to get an idea, follow this question and the accepted answer: django get complete url in query string

How to make Django url dispatcher use subdomain?

I have a vague idea on how to solve this, but really need a push :)
I have a Django app running with apache (mod_wsgi). Today urls look like this:
http://site.com/category/A/product/B/
What I would like to do is this:
http://A.site.com/product/B
This means that the url dispatcher some how needs to pick up the value found in the subdomain and understand the context of this instead of only looking at the path. I see two approaches:
Use .htaccess and rewrites so that a.site.com is a rewrite. Not sure if this does the trick since I don't fully understand what the django url dispatcher framework will see in that case?
Understanding how the url dispatcher DO work I could write a filter that looks at valid sub domains and provides this in a rewritten format to the url dispatcher code.
Any hints or solutions are very much appreciated! Thanks.
Have you looked at django.contrib.sites? I think a combination of that, setting SITE_ID in your settings.py, and having one WSGI file per "site" can take care of things.
EDIT: -v set.
django.contrib.sites is meant to let you run multiple sites from the same Django project and database. It adds a table (django.contrib.sites.models.Site) that has domain and name fields. From what I can tell, the name can mean whatever you want it to, but it's usually the English name for the site. The domain is what should show up in the host part of the URL.
SITE_ID is set in settings.py to the id of the site being served. In the initial settings.py file, it is set to 1 (with no comments). You can replace this with whatever code you need to set it to the right value.
The obvious thing to do would be to check an environment variable, and look up that in the name or domain field in the Site table, but I'm not sure that will work from within the settings.py file, since that file sets up the database connection parameters (circular dependency?). So you'll probably have to settle for something like:
SITE_ID = int(os.environ.get('SITE_ID', 1)
Then in your WSGI file, you do something like:
os.environ['SITE_ID'] = 2
and set that last number to the appropriate value. You'll need one WSGI file per site, or maybe there's a way to set SITE_ID from within the Apache setup. Which path to choose depends on the site setup in question.
The sites framework is most powerful where you use Site as the target of a ForeignKey or ManyToManyField so that you can link your model instances (i.e. records) to specific sites.
Mikes solution is correct if you want to have multiple sites with same apps with different content (sites module) on multiple domains or subdomains, but it has a drawback that you need to be running multiple instances of the Django process.
A better solution for the main problem about multiple domains or subdomains is to use a simple middleware that handles incoming requests with the process_request() function and changing the documented urlconf attribute (link) of the request object to the URLconf you want to use.
More details and an example of the per-request or per-domain URL dispatcher can be found at:
http://gw.tnode.com/0483-Django/
Try adding a wildcard subdomain: usually *.