Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: if regex.search(value): TypeError: expected string or bytes… #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sunwenquan
Copy link

Fix the following exception information.

"/Users/gavinsun/Research/Django/19.beautiful_app/django_app/user_sessions/templatetags/user_sessions.py", line 58, in device
    if regex.search(value):
TypeError: expected string or bytes-like object
[05/Jul/2018 15:52:06] "GET /account/sessions/ HTTP/1.1" 500 191771

Description

In user_sessions/templatetags/user_sessions.py,line 58,when value is None, python throws an exception. I added the following code in front to solve this problem.

    if not value:
        return None

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codecov
Copy link

codecov bot commented Jul 5, 2018

Codecov Report

Merging #87 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
+ Coverage   93.84%   93.88%   +0.04%     
==========================================
  Files          10       10              
  Lines         276      278       +2     
  Branches       31       32       +1     
==========================================
+ Hits          259      261       +2     
  Misses         11       11              
  Partials        6        6
Impacted Files Coverage Δ
user_sessions/templatetags/user_sessions.py 94.23% <100%> (+0.23%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9362ad6...67827ff. Read the comment docs.

@Bouke
Copy link
Collaborator

Bouke commented Jul 5, 2018

Can you explain why this change is needed? What caused user_agent to be None in the first place?

@sunwenquan
Copy link
Author

@Bouke
Copy link
Collaborator

Bouke commented Jul 11, 2018

That's not normal behaviour. Please investigate why the usage agent has not been set for that user. This could be caused by another issue altogether.

@SchrodingersGat
Copy link

@Bouke to raise this issue from the dead, I have found that this issue can arise when using the django testing framework to render a page - the user agent is not necessarily set

e.g.

def some_test(self):
    self.client.get("/page/")

leads to

![image](https://user-images.githubusercontent.com/10080325/181153596-242cc577-ce0b-4910-9beb-b4243561b411.png)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants