Not getting the expected result from Django reverse - django

hope anyone here will be able to help me out.
I am trying to get the reverse url for a viewset but somehow it's not working.
I will quickly go through what I have done from the moment everything was working fine to the moment the reverse stopped working.
I had a viewset on an app called "card" the reverse was reverse('card:card-list') everything was perfect, then I decided to move the card viewset to a different app called 'company' I did check the new urls structure by using the django_extensions and running the following command on the terminal python manage.py show_urls which gave me the new routing company:card-list I have then changed my reverse to reverse('company:card-list'), but now the code is broken and the reverse is not working as expected.
I have done those changes cause an end point as /api/company/card instead of /api/card/ .
Any ideas on how to solve it?
tks a lot

Related

How can I add atribute to webpage in django

screenshotI'm following a tutorial to create a website using django. I keep getting this error when running the server.
path('page2',index.webpage2),
AttributeError: module 'website.index' has no attribute 'webpage2'. Did you mean: 'webpage1'?
Don't know what I'm missing, the tutorial ran the server without any problem. The only thing I noticed was that it also gave my a missing os error. I fixed this part.
Thank you for your help
You need to add two views correspoending to the url
def webpage2(request):
pass
def webpage3(request):
pass

How to stop django from giving html pages as errors

I am working on an application with some friends and the back end REST API is in django. I sometimes get huge blocks of html printed to the console in place of anything meaningful, when I call an API from my angular front end. I have done some googling and I can't seem to find an answer of how to turn this off and make django return just error strings or json or something instead. Can someone help me get rid of this html?
Try using: https://docs.djangoproject.com/en/1.7/topics/logging/#configuring-logging
This will let you configure the logging in your django project.
Your Django app is working in debug mode. Please try this.
Go to ../yourdjangoproject/yourdjangoproject/settings.py
and find line Debug = True. Making it Debug=False will stop it from spitting out huge html upon errors.
Another thing you can do to only see errors as nice api response strings is this:
Find the view function which is giving the error, which can be found through the same huge html error message or by checking the view for url in urls.py
Then surround the whole view in try except like this.
def your_api_view(bla):
try:
#all of the view code goes here
except Exception as e:
return Response({"Error":e})
This way the error message will be shown to you like normal api response string.

POST method not allowed 405

'Add item' not working, symptom: 'POST method not allowed 405'. it is using 'create view'. 'add category' which bases on same class view works fine.
the error hasn't occur since I uploaded 2 weeks ago on Heroku, there is no use if I roll back to the point it is less complex and working.
i don't know what makes the app fail without changing code
add item works fine only in 2 categories 'hello-xinxin','songs life',i guess this means the code is good. other categories can't not be added in any new item. is this because database mess up?
The app: http://songs-todolist.herokuapp.com/todolist/
Source code: https://github.com/SongGithub/2DoList
The url you are trying to access is not mentioned in your urls.py of to_do_list_app.
That's why, it is failing.

i need to restart django server to make my app properly work

so i made a python script to grab images from a subreddit (from Imgur and imgur albums). i successfully done that (it returns img urls) and wanted to integrate it into django so i can deploy it online and let other people use it. when i started running the server at my machine, the images from subreddit loads flawlessly, but when i try another subreddit, it craps out on me (i'll post the exception at the end of the post). so i restart the django server, and same thing happen. the images loads without a hitch. but the second time i do it, it craps out on me. what gives?
Exception Type: siteError, which pretty much encompasses urllib2.HTTPError, urllib2.URLError, socket.error, socket.sslerror
since i'm a noob in all of this, i'm not sure what's going on. so anyone care to help me?
note: l also host the app on pythoneverywhere.com. same result.
Using a global in your get_subreddit function looks wrong to me.
reddit_url = 'http://reddit.com/r/'
def get_subreddit(name):
global reddit_url
reddit_url += name
Every time, you run that function, you append the value of name to a global reddit_url.
It starts as http://reddit.com/r/
run get_subreddit("python") and it changes to http://reddit.com/r/python
run get_subreddit("python") again, and it changes to http://reddit.com/r/pythonpython
at this point, the url is invalid, and you have to restart your server.
You probably want to change get_subreddit so that it returns a url, and fetch this url in your function.
def get_subreddit(name):
return "http://reddit.com/r/" + name
# in your view
url = get_subreddit("python")
# now fetch url
There are probably other mistakes in your code as well. You can't really expect somebody on stack overflow to fix all the problems for you on a project of this size. The best thing you can do is learn some techniques for debugging your code yourself.
Look at the full traceback, not just the final SiteError. See what line of your code the problem is occurring in.
Add some logging or print statement, and try and work out why the SiteError is occurring.
Are you trying to download the url that you think you are (as I explained above, I don't think you are, because of problems with your get_subreddit method).
Finally, I recommend you make sure that the site works on your dev machine before you move on to deploying it on python anywhere. Deploying can cause lots of headaches all by itself, so it's good to start with an app that's working before you start.
Good luck :)

django database sync problem

In my web django, I do a query of Models.objects.all() or actually any other models in the views.py. It returns me a [,] value (empty array) - view from logs. However back in the manage.py shell, it is able to retrieve the objects. In mysql database, the data entries are there!
I have no idea how to debug from here, is there a way to resolve this error? This is not my first time. I have been doing django project for several months already. Only today, it is giving me this problem.
EDIT : This problem is very weird, I'm just curious whether anybody has encountered this problem before, I'm using Eclipse to edit the scripts(views.py, models.py etc).
Do you have __repr__ defined for your objects? Take a look at the HTML source generated by Django -- there's a good chance it looks like:
[<Model: model object>, <Model: model object>, ...]
Your web browser then interprets the <...> as an HTML tag that it doesn't understand, and thus can't render, and displays only the content outside the tags (the "[", ",", and "]" characters).