I am very new at Django. I am following the tutorial by Vitor Freitas, "A Complete Beginner's Guide to Django". In the test module, I get the error shown below. Can some kind sole guide me as to my error.
ERROR: test_redirection (boards.tests.test_view_reply_topic.SuccessfulReplyTopicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/hp/Desktop/PythonProjects/myproject/myproject/boards/tests/test_view_reply_topic.py", line 34, in test_redirection
self.assertRedirects(self.response, topic_posts_url)
AttributeError: 'SuccessfulReplyTopicTests' object has no attribute 'response'
The file it is referring to is this file
class SuccessfulReplyTopicTests(ReplyTopicTestCase):
def test_redirection(self):
url = reverse('topic_posts', kwargs={'pk': self.board.pk, 'topic_pk': self.topic.pk})
topic_posts_url = '{url}?page=1#2'.format(url=url)
self.assertRedirects(self.response, topic_posts_url)
=====================================================
FAIL: test_redirection (accounts.tests.test_view_signup.SuccessfulSignUpTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/hp/Desktop/PythonProjects/myproject/myproject/accounts/tests/test_view_signup.py", line 62, in test_redirection
self.assertRedirects(self.response, self.home_url)
File "/usr/local/lib/python3.7/dist-packages/django/test/testcases.py", line 348, in assertRedirects
% (response.status_code, status_code)
AssertionError: 200 != 302 : Response didn't redirect as expected: Response code was 200 (expected 302)
The file it is referring to is this file
class SuccessfulSignUpTests(TestCase):
def test_redirection(self):
self.assertRedirects(self.response,self.home_url)
Based on the code here : https://simpleisbetterthancomplex.com/series/2017/10/09/a-complete-beginners-guide-to-django-part-6.html
1 - Do you have this part written correct ?
class LoginRequiredPostUpdateViewTests(PostUpdateViewTestCase):
def test_redirection(self):
'''
Test if only logged in users can edit the posts
'''
login_url = reverse('login')
response = self.client.get(self.url)
self.assertRedirects(response, '{login_url}?next={url}'.format(login_url=login_url, url=self.url))
2- Obviously the code has many errors, and there is comments about it on the post, have you checked them all ?
3- Have you checked the code in github ?
Best of luck!
Related
I have defined the following function to get the redirected URLs using Requests library. However i get the error KeyError: 'location'
def get_redirected_url(r_url):
r = requests.get(r_url, allow_redirects=False)
url = r.headers['Location']
return url
Calling the function
get_redirected_url('http://omgili.com/ri/.wHSUbtEfZQujfav8g98PjRMi_ogV.5EwBTfg476RyS2Gqya3tDAwNIv8Yi8wQ9AK4.U2mxeyq2_xbUjqsOx8NYY8r0qgxD.4Bm2SrouZKnrg1jqRxEfVmGbtTaKTaaDJtOjtS46fYr6A5UJoh9BYxVtDGJIsbSfgshRXR3FVr4-')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in get_redirected_url
File "/home/user/PycharmProjects/untitled/venv/lib/python3.6/site-packages/requests/structures.py", line 54, in __getitem__
return self._store[key.lower()][1]
KeyError: 'location'
Is it failing because the redirection waits for 5 seconds? If so, how do we incorporate that as well?
I have tried the other answers like this and this. But unable to crack it.
It is simple: r.headers doesn't have 'Location' key. You may have use the wrong key.
Edit: the site you want to browse with requests is protected.
I have the following code:
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=9bZkp7q19f0')
print(yt.title)
And am being thrown the following error
Traceback (most recent call last):
File "C:\Users\tom\Desktop\Desktop\archiver.py", line 4, in <module>
print(yt.title)
File "C:\Users\tom\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pytube\__main__.py", line 254, in title
return self.player_config_args['title']
KeyError: 'title'
What is going wrong here?
A lot of people having this issue, one suggested thing to try is to make the following changes mentioned in this pull request.
https://github.com/nficano/pytube/pull/435/files.
I'm trying to learn mechanize to create a chat logging bot later, so I tested out some basic code
import mechanize as mek
import re
br = mek.Browser()
br.open("google.com")
However, whenever I run it, I get this error.
Traceback (most recent call last):
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 262, in _mech_open
url.get_full_url
AttributeError: 'str' object has no attribute 'get_full_url'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 5, in <module>
br.open("google.com")
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 253, in open
return self._mech_open(url_or_request, data, timeout=timeout)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/mechanize/_mechanize.py", line 269, in _mech_open
raise BrowserStateError("can't fetch relative reference: "
mechanize._mechanize.BrowserStateError: can't fetch relative reference: not viewing any document
I double checked with the documentation on the mechanize page and it seems consistent. What am I doing wrong?
You have to use a schema, otherwise mechanize thinks you are trying to open a local/relative path (as the error suggests).
br.open("google.com") should be br.open("http://google.com").
Then you will see an error mechanize._response.httperror_seek_wrapper: HTTP Error 403: b'request disallowed by robots.txt', because google.com does not allow crawlers. This can be remedied with br.set_handle_robots(False) before open.
I am doing a test to check the contents of a csv file using assertContains():
response = client.get('/abc/1/a_b_csv')
print(response.content)
self.assertContains(response.content, 'aakash')
I tried different options such as self.assertContains(response,'aakash')
but didn't get any result.
My csv file looks like,
Name Age
Aakash 22
Sometimes there is the error:
bName\r\nVipul\r\n'
E
======================================================================
ERROR: test_csv (timepay.new_test.ReportTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/aakash/Projects/test.py", line 28, in test_csv
self.assertNotContains(response.content, 'Vipul')
File "/home/aakash/Projects/project_env/lib/python3.6/site-packages/django/test/testcases.py", line 402, in assertNotContains
response, text, status_code, msg_prefix, html)
File "/home/vipul/Projects/project_env/lib/python3.6/site-packages/django/test/testcases.py", line 355, in _assert_contains
response.status_code, status_code,
AttributeError: 'bytes' object has no attribute 'status_code'
Looking at the stacktrace you added to the question, the stacktrace corresponds to the call to self.assertNotContains(response.content, 'Vipul').
Looking at the docs for SimpleTestCase.assertNotContains(), I notice that the method expects the whole response, not just the content as parameter.
So, changing this line:
self.assertNotContains(response.content, 'Vipul')
to this
self.assertNotContains(response, 'Vipul')
should clear up the error for which you added the stacktrace.
Now, I notice that you also have this line in your question:
I tried different options such as self.assertContains(response,'aakash'), but didn't get any result.
You will have to be more specific as to what you mean by "didn't get any result" if you want us to be able to help you.
I am starting working on GraphQL and as I am from python background I am using GraphQL with Python.
I followed the steps provided here Link but I am still facing issues.
An error occurred while resolving field Query.hello
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/graphql/execution/executor.py", line 311, in resolve_or_error
return executor.execute(resolve_fn, source, info, **args)
File "/usr/local/lib/python3.5/dist-packages/graphql/execution/executors/sync.py", line 7, in execute
return fn(*args, **kwargs)
TypeError: resolve_hello() missing 2 required positional arguments: 'context' and 'info'
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/graphql/execution/executor.py", line 330, in complete_value_catching_error
exe_context, return_type, field_asts, info, result)
File "/usr/local/lib/python3.5/dist-packages/graphql/execution/executor.py", line 383, in complete_value
raise GraphQLLocatedError(field_asts, original_error=result)
graphql.error.located_error.GraphQLLocatedError: resolve_hello() missing 2 required positional arguments: 'context' and 'info'
None
Please, help me resolve the issue.
It looks like the graphQL documentation is out of date. Graphene-python 2 changed the method signature. Try something like this instead
def resolve_hello(self, info, **kwargs):
return 'Hello world!'
You are not giving much information here, maybe the code that triggers the error would be helpful, but googling I found some related posts
https://github.com/graphql-python/graphene/issues/601
https://github.com/graphql-python/graphene-django/issues/282
Maybe check the versions that you are using as they mention on the first link
Try to like this one concept example on date:
Graphql required positional arguments
info,and **kwarg
def resolve_generated_date(self, info, **kwargs):
created_date = self.created_at.strftime('%Y-%m-%d')
return created_date