Building for the Splunk Platform

How edit my code to display the results of a file in Splunk using Django bindings?

Federica_92
Communicator

Hi everyone,

I'm trying to display the results of a file in Splunk using the django bindings, but something is wrong in my code. Could someone help me?

URL.py

url(r'^mypage/$', 'mynewapp.views.mypage_view', name='mypage'),

views.py

@render_to('mynewapp:mypage.html')
@login_required
def mypage_view(request):
file_data = ''
service = request.service
with open(csvfile, "r+") as lines:      
for line in lines:
    file_data += line
     return file_data

mypage.html

   {% for data in file_data %}
    <p>{{ data }}</p>
    {% endfor %}
0 Karma

lmyrefelt
Builder

Just wanted to update with some progress ..
ok, so after changing my urls.py for my app from ;

url(r'^mypage/$', 'mynewapp.views.mypage_view', name='mypage'),

to

url(r'^mypage/$', render('mynewapp:mypage.html'), name='mypage'),

My urls.py and "views.py" are now working.

I am still not able to display some of the contents from my views.py in my "mypage", however i can see that it gets the list of apps from splunk at least and iterate over it, but still does not display the results on mypage.

a small step for mankind but a huge leap forward for lmyrefelt 🙂

0 Karma

Federica_92
Communicator

cool : ) Can I ask you when you will finish, how set a condition to execute the render? Because for now (that I have used: url(r'^pythondemo/$', 'mynewapp.views.pythondemo_view', name='pythondemo'), like in the splunk demo ) work in the moment that I open my app, but I would like that this will work only after a choose in the dropdown menu.

0 Karma

lmyrefelt
Builder

So you got the example(s) to work? if i do use the example(s) code in dev.splunk.com for the django app i am only getting this message over and over again.

This XML file does not appear to have any style information associated with it. The document tree is shown below.

I also get these ones (if some devs are dropping by) :

2015-02-25 11:05:11,968 ERROR urls:68 - invalid syntax (urls.py, line 13) Traceback (most recent call last): File "/opt/splunk/dev/6.2.1/splunk/etc/apps/framework/server/urls.py", line 61, in load_urls_for_app module_with_urls = importlib.import_module("%s.urls" % app) File "/opt/splunk/dev/6.2.1/splunk/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module __import__(name) File "/opt/splunk/dev/6.2.1/splunk/etc/apps/filex/django/filex/urls.py", line 13 ^ SyntaxError: invalid syntax

As soon as i change (urls.py) as above i get the urls to work and no more error message as above

0 Karma

Federica_92
Communicator

Yes, with the example works, but if I try to change the name of the variable, anything doesn't work.
Try to look at this: http://dev.splunk.com/view/SP-CAAAEMP

0 Karma

lmyrefelt
Builder

Thanks, thats the page and examples i am trying to follow ... i am feeling so stupid not to even be able to get the examples to work 😛

0 Karma

Federica_92
Communicator

Don't worry, I had the same problem XD
Let me know if changing the variables it start to work, like use instead of "pythondemo" "mypage", something like this.

0 Karma

lmyrefelt
Builder

OK, so i have manage to start to get the views.py working ... as i am a newbie with python ... i have not yet hammered the "indention" into the back of my head !
That seems to have been a big part of at least my problems!

Maybe that is something you should verify as well ? You should be able to find the messages under; sourcetype=djange_service

0 Karma

Federica_92
Communicator

Yeah, I'm new in all this too : )
Sorry, I'm not sure of what you are asking, to me, could you said it again?

0 Karma

lmyrefelt
Builder

You might need to put your code more "inline" that is add a tab to the beginning of the code

Example (compare to your pasted code above;

@render_to('mynewapp:mypage.html')
 @login_required
 def mypage_view(request):
          file_data = ''
          service = request.service
          with open(csvfile, "r+") as lines:        
               for line in lines:
                     file_data += line
               return file_data

}

or Similar.

You should be able to find an (python) error message in index=_internal sourceype="django*"

0 Karma

Federica_92
Communicator

Sure, sorry,
I'm using this indention now:

@render_to('mynewapp:mypage.html')
@login_required
def mypage_view(request):
    file_data = ''
    service = request.service
    with open(csvfile, "r+") as lines:
        for line in lines:
            file_data += line
return file_data

For each type of error that I do, I have ALWAYS this error: Internal server error.
However, my problem is not the inline of the code, like this works.
I found problem when I change the name of my file from pythondemo to mypage

0 Karma

lmyrefelt
Builder

don't forget the } at the end

}

0 Karma
Get Updates on the Splunk Community!

Enterprise Security Content Update (ESCU) v3.54.0

The Splunk Threat Research Team (STRT) recently released Enterprise Security Content Update (ESCU) v3.54.0 and ...

Using Machine Learning for Hunting Security Threats

WATCH NOW Seeing the exponential hike in global cyber threat spectrum, organizations are now striving more for ...

New Learning Videos on Topics Most Requested by You! Plus This Month’s New Splunk ...

Splunk Lantern is a customer success center that provides advice from Splunk experts on valuable data ...