Django running wrong view method - django

I have to view files stored in mysite folder. one is named as views.py and other is named as request_view.py. In urls.py, I have used 'answer' method for views.py and 'display_meta' method for request_view.py.
(django version: 1.5 and python version: 2.7.3)
this is the url pattern:
url(r'^twitter/$', answer), url(r'request/$', display_meta)
when I call http:/127.0.0.1:8000/request/, then also first view(i.e. /twitter/) is called!
any help?
one more thing. In my view.py, I have some unbounded code (i.e. the code which is neither present in a method or class). can this be the cause of the problem?
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
keyword = input('enter the keyword you want to search for?')
stream.filter(track = [keyword])
apart from this code, evry code is either in the class or method.
One thing that I noticed is that first the code of the view.py runs, then display_meta runs.
Thanks in advance.
SOLVED
The problem was with the import function that I was using. since my code was unbounded in one of the views, the import function always import that regardless of the url that I choose.
Suggestion
Always use the nomenclature mentioned in the this example. In many books it has been suggested that we should import the views, but it might cause an error if you have unbounded code like I had.

I don't know exactly why /twitter/ view is called, but I can see two things to change:
You should use a string as the second parameter for url(), as you can see in this example [1]. You can use 'myapp.views.my_method' nomenclature.
You forgot to start the request URL with ^ that indicates the start of the URL.
About the unbounded code, I don't know if that could be causing the problem. But I can't see why are you putting that code unbounded. I am not sure when that code would be executed, I guess the first time you call a view in that file and Django loads the file (I'm guessing, I don't know exactly), but I don't think that would be a good way to do that. Think when do you want to execute that code, put it in a method, and call it.
[1] https://docs.djangoproject.com/en/1.5/topics/http/urls/#example

HI hemant i am wondering why you have written request_view.py.
Please see the django docs.
what you can do is .
Create two function in your views.py like
def answer(request):
do some stuffs.
render_to_response(template.html)
and on the same page write another
def display_meta(request):
# do your studd
render_to_response(some.html)
YOU NEED NOT TO CREATE TWO SEPERATE VIEWS.PY
I dont know what this code does
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
keyword = input('enter the keyword you want to search for?')
stream.filter(track = [keyword])
But if you want to use StdOutListener inside your function you can call in your view
like
def display_meta(request):
stobject = StdOutListener() # use parameters if you have here
# do your studd
render_to_response(some.html)

Organizing views into a python package could solve this problem. So if you have a structure like this...
# views.py
def SomeView(request):
return HttpResponse('SomeView')
# another_view.py
def AnotherView(request):
return HttpResponse('AnotherView')
Your can reorganize these separate view files into a views package. That is...
# views
# __init__.py
from views import SomeView
from another_view import AnotherView
# views.py
def SomeView(request):
return HttpResponse('SomeView')
# another_view.py
def AnotherView(request):
return HttpResponse('AnotherView')
And now, everything can be called in a django-standard way:
url(r'^url-to-some-view/$', 'views.SomeView'),
url(r'^url-to-another-view/$', 'views.AnotherView'),
UPDATED:
To make a 'python package'...
Create a views directory at the same level as the view.py file [mkdir views]
Create a __init.py__ file inside the views directory # this is what makes a directory a 'python package'
Move views.py into the views directory.
Move your request_view.py into the views directory.
Edit the __init__.py file with the necessary import statements. In this case:
from views import answer
from request_view import display_meta
What this does is replace a file with a directory. By importing everything into the __init__.py file, this directory looks like a large file to your code, rather than another module.

Related

Relative links to admin.py

There is a class in admin.py:
class NewsAdmin(TranslationAdmin):
form = NewsForm
list_display = ('title', 'date' 'show_url')
def show_url(self, obj):
return format_html("<a href='http://www.host.ru/news/{url}' target='_blank'>view</a>", url=obj.id)
show_url.short_description = "View"
Need a link http://www.host.ru replace with a relative one, something like this: protocol + domain name + news/{url}
How can this be done?
I think you are better off making it hardcoded value in your settings, like BASEURL = 'http://www.host.ru'
then having different settings.py for production and development (dev with BASEURL = 'https://localhost:8080' for testing)
and then you can fetch it with:
def show_url(self, obj):
from django.conf import settings
return format_html("<a href='{base}/news/{url}' target='_blank'>view</a>", base=settings.BASEURL, url=obj.id)
# or use reverse so it's dynamically linked to your urls.py (this is what I do)
def show_url_with_reverse(self, obj):
from django.conf import settings
from django.urls import reverse
return format_html("<a href='{base}{url}' target='_blank'>view</a>",
base=settings.BASEURL,
url=reverse('myviewname', args=[obj.id])
)
This isn't the answer you wanted, but I'm throwing it in as a way. I hope someone smarter than me has found a better way, but this is a fallback.
Reasoning
This is all moot because you don't have access to the request object inside that method.
I've found that you need the request object to dynamically get the host, the www.host.ru part, with request.get_host().
But that still leads the http:// and https://, which is not available anywhere (that I've found), so I have that hardcoded in my settings.py.
So either way something will have have to be hardcoded, so might as well be the entire url. It's gross, I hate it, but w/e it gets the job done

Registering Django system checks in AppConfig's ready() method

In the docs for Django's System check framework it says:
Checks should be registered in a file that’s loaded when your application is loaded; for example, in the AppConfig.ready() method.
None of the examples on that page, or around the AppConfig.ready() method, show how to do this. Given a checking method like:
from django.core.checks import register, Tags
#register(Tags.compatibility)
def my_check(app_configs, **kwargs):
# ... perform compatibility checks and collect errors
return errors
How would you do this in/from the AppConfig.ready() method? Is one called from the other? Which file should the above method go in? Do you add #register(...) to the ready() method?
From reading the examples on this page about the Apps Registry and System Checks Framework, it seems there are (at least) two ways to add your own System Checks. To adapt that page's examples (assuming you're creating an app called myapp):
1) Create a myapp/checks.py file like this:
from django.apps import apps as camelot_apps
from django.core.checks import register, Warning
from django.core.checks import Tags as DjangoTags
class Tags(DjangoTags):
"""Do this if none of the existing tags work for you:
https://docs.djangoproject.com/en/1.8/ref/checks/#builtin-tags
"""
my_new_tag = 'my_new_tag'
#register(Tags.my_new_tag)
def check_taggit_is_installed(app_configs=None, **kwargs):
"Check that django-taggit is installed when usying myapp."
errors = []
try:
from taggit import models
except ImportError:
errors.append(
Warning(
"The django-taggit app is required to use myapp.",
hint=("Install django-taggit"),
# A unique ID so it's easier to find this warning:
id='myapp.W001',
)
)
return errors
And then in myapp/__init__.py (create it if it doesn't exist):
from . import checks
Running this should run that check above:
$ ./manage.py check myapp
2) Or, as I thought in my initial question, you can register that check in your AppConfig. So, keep the above code in myapp/check.py, but remove the #register(Tags.my_new_tag) line.
Then create myapp/apps.py containing this:
from django.core.checks import register
from .checks import Tags, check_taggit_is_installed
class MyappConfig(AppConfig):
name = 'myapp'
def ready(self):
super(MyappConfig, self).ready()
register(Tags.my_new_tag)(check_taggit_is_installed)
And alter myapps/__init__.py so it contains this:
from . import checks
default_app_config = 'myapp.apps.MyappConfig'
The first example seems simpler, with no need for a custom AppConfig.
Django recommends:
Checks should be registered in a file that’s loaded when your application is loaded; for example, in the AppConfig.ready() method.
Therefore, place the checks code in a checks.py file. Then simply in apps.py, as with signals:
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'MyApp'
verbose_name = "My App"
def ready(self):
# import myapp.signals
import myapp.checks
My guess is that you can just place this code in ready() method. For example of usage you can take a look at django.contrib.auth (or other contrib app). This app has check in checks.py file, that imported in apps.py and registered in ready() method: checks.py, apps.py.

Dynamic image upload/browsing path for django-tinymce

I would like to use tinyMCE as the editor for my django application, but have run into some trouble. I have everything setup, but it appears there is no way to specify the upload path for the image insert/upload function. I have two specific scenarios where this becomes a problem:
File browser for user submitted content should not show files uploaded by other users. I'd like to "jail" them to a specific directory by user id.
I need a way to link uploaded files with the object they are related to. That way I can prune those files in the future if the object no longer exists, and I can show only images that are related to that object in the filebrowser.
django-tinymce-filebrowser automatically sets the upload path to mce_filebrowser/%Y/%m/%d. There doesn't appear to be an option to change this path in any way.
django-filebrowser has options for setting the upload directory in settings.py, but I haven't been able to find any record of someone overriding that path for a specific modelform instance. The closest I found was Django filebrowser, model specific directory parameter for FileBrowserField, but I think the solution there isn't applicable to my situation.
Is anyone using another filebrowser for django-tinymce? Did you have a similar problem and find a fix for it? I'd appreciate any points in the right direction.
I used a similar approach but instead of modifying the django-filebrowser code I ended up extending the browse() method in a subclass of FileBrowserSite and making the modification there:
from django.core.files.storage import DefaultStorage
from filebrowser.sites import FileBrowserSite
class FileBrowserSite(FileBrowserSite):
def browse(self, request):
self.directory = self.directory + str(request.user) + '/'
return super(FileBrowserSite, self).browse(request)
storage = DefaultStorage()
site = FileBrowserSite(name='file', storage=storage)
site.directory = "content/"
I put this piece of code on a file called filebrowser.py and the then on my urls.py I did:
from .filebrowser import site
urlpatterns = [
url(r'^admin/content/file/', include(site.urls)),
]
I think is a much cleaner approach than modifying the source code, and is working like charm on my project.
I've extended a little bit the answer by Erasmo. Generally, it works great. Thanks! However, as OriolJ pointed out, every user needs a created directory to use the filebrowser. It is recommended to avoid using signals, so I added the functionality to the custom FileBrowserSite.
filebrowser.py
import os
from django.conf import settings
from django.core.files.storage import DefaultStorage
from filebrowser.sites import FileBrowserSite
class FileBrowserSite(FileBrowserSite):
def browse(self, request):
# get directory path from settings to avoid recursion
self.directory = settings.DIRECTORY + str(request.user) + '/'
# create a directory for a user if it does not already exist
full_path = self.storage.location + '/' + self.directory
if not os.path.exists(full_path):
os.makedirs(full_path)
return super().browse(request)
storage = DefaultStorage()
site = FileBrowserSite(name='file', storage=storage)
settings.py
FILEBROWSER_DIRECTORY = 'user_content/'
DIRECTORY = ''
urls.py
from .filebrowser import site
urlpatterns = [
url(r'^admin/content/file/', include(site.urls)),
]
Hope this slight update will save someone a couple of minutes.
I've, somewhat hacked this. I'm on grappelli 2.7.2, and django-filebrowser 3.6.1.
I've just added a line to the django-filebrowser file: sites.py:
on the method browse() of the class FileBrowserSite, line ~273, I've added:
self._directory_set(DIRECTORY + str(request.user) + "/")
It's important though that there is already a directory created for every user, you can make a post_save signal to create a directory every time a user is created. This solution will not relate the files with the object, but I think that this may be a good start for you and it will isolate files of different users.
For me, this is currently working just fine, I can imagine that this is not the most perfect way to do it, feedback is very welcome.

Renaming django FileField files

Some additional features were added to a django application, and as a result the upload_to function also expanded.
Since django by default stores filenames in the database, and files on disk, no harm has been done - new files are named using a new upload_to function, while old files continue to work.
However, this is messy - we end up in a situation with files like
/media/userID/oldfilename.pdf
and
/media/app/userID/projectID/newfilename.pdf
Is there a way to bulk rename those files? I guess it could be done by iterating through the database, checking if the path in FileField matches the result of current upload_to, and if not, rename.. it seems like a common problem, so perhaps there is a more generic way out there?
The simple solution is to write a custom Django management command. You can run the command using Django's standard manage.py.
Something like this:
from django.core.management.base import BaseCommand, CommandError
from example.models import YourModel
class Command(BaseCommand):
args = ''
help = ''
def handle(self, *args, **options):
# Get all objects
objects = YourModel.objects.all()
for object in objects: # For each object
# If old file path:
if not 'userID/projectID' in objects.filefield.name:
# Move the file, eg usign shutil http://docs.python.org/library/shutil.html#shutil.move
# Update filefield
# save object

How can I use django.core.files.File

The docs for django.core.files.File imply I can do this: print File(open(path)).url but the File object has no attribute 'url' However, django.db.models.fields.files.FieldFile extends File and does have all the attributes described in the docs for File, but I can't create one without giving it a model field.
All I want it something that does what the docs for django.core.files.File (link above) say it does, take a python file and give it attributes like 'url' and 'path' and 'name', can anyone help?
Cheers, Jake
Regardless of what the Django doc says, if you look at the code for the File class, I don't see it there. Following Ignacio's suggestion, you can derive from the Django File and use the MEDIA_ROOT and MEDIA_URL settings to implement the property you're looking for...
from django.core.files import File
from django.conf import settings
class UrlFile(File):
def _get_url(self):
root_name = self.name.replace(settings.MEDIA_ROOT, '')
return '%s%s' % (settings.MEDIA_URL, root_name)
url = property(_get_url)
If you derive from file then you can give it any attributes you like:
class MyFile(file):
def foobar(self):
print 'foobar'
f = MyFile('t.txt', 'r')
f.foobar()
Thanks Guys. I've solved my problem by writing a custom class, it's not as powerful as the Django one would be if I could use it, but it works for my current application. I'll open a ticket about the docs.